What is the advantage of linspace over the colon “:” operator?

前端 未结 3 1423
小鲜肉
小鲜肉 2020-11-29 09:06

Is there some advantage of writing

t = linspace(0,20,21)

over

t = 0:1:20

?

I understand the fo

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-29 09:58

    linspace is useful where you know the number of elements you want rather than the size of the "step" between them. So if I said make a vector with 360 elements between 0 and 2*pi as a contrived example it's either going to be

    linspace(0, 2*pi, 360)
    

    or if you just had the colon operator you would have to manually calculate the step size:

    0:(2*pi - 0)/(360-1):2*pi
    

    linspace is just more convenient

    For a simple real world application, see this answer where linspace is helpful in creating a custom colour map

提交回复
热议问题