Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

arrays - What is the advantage of linspace over the colon ":" operator?

Is there some advantage of writing

t = linspace(0,20,21)

over

t = 0:1:20

?

I understand the former produces a vector, as the first does.
Can anyone state me some situation where linspace is useful over t = 0:1:20?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's not just the usability. Though the documentation says:

The linspace function generates linearly spaced vectors. It is similar to the colon operator :, but gives direct control over the number of points.

it is the same, the main difference and advantage of linspace is that it generates a vector of integers with the desired length (or default 100) and scales it afterwards to the desired range. The : colon creates the vector directly by increments.

Imagine you need to define bin edges for a histogram. And especially you need the certain bin edge 0.35 to be exactly on it's right place:

edges = [0.05:0.10:.55];
X = edges == 0.35

edges =   0.0500    0.1500    0.2500    0.3500    0.4500    0.5500
X =  0     0     0     0     0     0

does not define the right bin edge, but:

edges = linspace(0.05,0.55,6);   %// 6 = (0.55-0.05)/0.1+1
X = edges == 0.35

edges =   0.0500    0.1500    0.2500    0.3500    0.4500    0.5500
X =  0     0     0     1     0     0

does.

Well, it's basically a floating point issue. Which can be avoided by linspace, as a single division of an integer is not that delicate, like the cumulative sum of floting point numbers. But as Mark Dickinson pointed out in the comments: You shouldn't rely on any of the computed values being exactly what you expect. That is not what linspace is for. In my opinion it's a matter of how likely you will get floating point issues and how much you can reduce the probabilty for them or how small can you set the tolerances. Using linspace can reduce the probability of occurance of these issues, it's not a security.

That's the code of linspace:

n1 = n-1
c = (d2 - d1).*(n1-1) % opposite signs may cause overflow
if isinf(c)
    y = d1 + (d2/n1).*(0:n1) - (d1/n1).*(0:n1)
else
    y = d1 + (0:n1).*(d2 - d1)/n1
end

To sum up: linspace and colon are reliable at doing different tasks. linspace tries to ensure (as the name suggests) linear spacing, whereas colon tries to ensure symmetry

In your special case, as you create a vector of integers, there is no advantage of linspace (apart from usability), but when it comes to floating point delicate tasks, there may is.

The answer of Sam Roberts provides some additional information and clarifies further things, including some statements of MathWorks regarding the colon operator.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...