Q&A
Ask and answer questions to make information more available to wider audiences.
Calvin Blount @blountcalvin   22, Aug 2023 12:00 AM
difference between linspace vs the colon : operator for creating vectors
What's the difference between linspace vs the colon : operator for creating vectors in MATLAB?
answers 1
 
Answer 1
Drake Boot @bootdrake85   31, Aug 2023 03:22 PM
linspace and the colon operator ; do different things:

linspace creates a vector of the specified length, and then scales it down to the specified interval with a division. In this way it ensures that the output vector is as linearly spaced as possible.

The colon operator (:) adds increments to the starting point, and subtracts decrements from the end point to reach a middle point. In this way, it ensures that the output vector is as symmetric as possible.

The two methods thus have different aims, and will often give very slightly different answers, e.g.

>> a = 0:pi/1000:10*pi;
>> b = linspace(0,10*pi,10001);
>> all(a==b)
ans =
    0
>> max(a-b)
ans =
  3.5527e-15
In practice, however, the differences will often have little impact unless you are interested in tiny numerical details. linspace is more convenient when the number of gaps is easy to express, whereas the colon operator : is more convenient when the increment is easy to express.