There are two principal ways to create vectors in MATLAB. One is the *|(:)|* operator, and the other is the *|linspace|* function. The principal difference is that with the *|colon|* operator, you define the _interval_ between successive elements and let the _length_ of the resulting vector vary, and in *|linspace|*, you define the _length_ of the vector and the function calculates the _interval_ to fit the length. Answer from Star Strider on mathworks.com
Discussions

When to use Linspace?
Your signal on the top is aliasing! Note that your frequency is 100hz, so one period is equal to 1/100= 0.01 seconds, or completes 10 cycles every 0.1 seconds (look at the 3rd plot). I generally avoid linspace since I like using my own precision (i.e. t=beginning:precision:ending), and for continuous time I sometimes set precision = (f*10)-1 (so I see 10 points every period). More on reddit.com
🌐 r/matlab
4
1
February 17, 2021
Plotting sine functions using linspace command
Plotting sine functions using linspace command. Learn more about plotting MATLAB More on mathworks.com
🌐 mathworks.com
2
0
February 25, 2024
arrays - What is the advantage of linspace over the colon ":" operator? - Stack Overflow
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 u... More on stackoverflow.com
🌐 stackoverflow.com
What is the difference between a vector and using linspace?
What is the difference between a vector and... Learn more about linspace, arrays MATLAB More on mathworks.com
🌐 mathworks.com
1
1
August 31, 2015
🌐
Johns Hopkins University
math.jhu.edu › ~shiffman › 370 › help › techdoc › ref › linspace.html
linspace (MATLAB Function Reference)
Description The linspace function generates linearly spaced vectors. It is similar to the colon operator ":", but gives direct control over the number of points. y = linspace(a,b) generates a row vector y of 100 points linearly spaced between a and b.
🌐
Northwestern University
ece.northwestern.edu › local-apps › matlabhelp › techdoc › ref › linspace.html
linspace (MATLAB Functions)
y = linspace(a,b) generates a row vector y of 100 points linearly spaced between and including a and b.
🌐
University of Utah
users.cs.utah.edu › ~germain › PPS › Topics › Matlab › linspace.html
Matlab Function - Linspace
The linspace function produces an array or matrix filled with a requested number of numbers starting at a given value and ending at a given value.
🌐
Reddit
reddit.com › r/matlab › when to use linspace?
r/matlab on Reddit: When to use Linspace?
February 17, 2021 -

I'm working with continuous-time (CT) and discrete-time (DT) signal and want to plot them in Matlab.

For CT, when I used the function linspace to create a vector of values and then use that to have a function (t1, x1 and t2, x2). I then plot the value of x() against the created t(). The graphs I got were different with different length of my vector generated by linspace and none of them seems correct. (The correct version is using the normal method* of creating vector (t3, x3)).

Code:

f=100;                     %Frequency
t1=linspace(0,1,100);      %From 0 to 1 in 100 steps
t2=linspace(0,1,300);      %From 0 to 1 in 300 steps
t3=0:0.001:1;              %From 0 to 1 and increment by 0,001

x1=cos(2*pi*f*t1);
x2=cos(2*pi*f*t2);
x3=cos(2*pi*f*t3);

subplot(3,1,1);
plot(t1,x1);
subplot(3,1,2);
plot(t2,x2);
subplot(3,1,3);
plot(t3,x3);

The same problem does not apply whether or not I use linspace or normal method* to create an array to plot magnitude and phase of a function.

Code:

w=linspace(-10*pi,10*pi,1000);
xa=(i*w)./(1+i*w);
figure(1);
subplot(2,1,1);plot(w,abs(xa));grid on;
subplot(2,1,2);plot(w,angle(xa));grid on;

If I generate w by normal method*, the graph still looks the same.

Can someone please explain!

Thanks.

Graphs of (t1, x1) (t2, x2) (t3, x3)
🌐
GeeksforGeeks
geeksforgeeks.org › software engineering › linearly-spaced-vector-in-matlab
Linearly Spaced Vector in MATLAB - GeeksforGeeks
November 28, 2022 - More clearly, say one wants to divide a domain [1,2] in intervals with 5 points or vectors so, the resultant vector would be [1.0, 1.25, 1.50, 1.75, 2.0]. This new vector is what we call a linearly spaced vector.
Find elsewhere
🌐
EDUCBA
educba.com › home › data science › data science tutorials › matlab tutorial › linspace matlab
Linspace MATLAB | Different function of linspace in matlab with examples
March 24, 2023 - The linspace function in MATLAB provides us with an array/matrix comprising the desired number of values starting from and ending at a declared value. The produced array will have exactly the desired number of terms which will be evenly spaced.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
YouTube
youtube.com › joshtheengineer
Explained: Linspace [MATLAB] - YouTube
In this video I show you how to use the linspace function in MATLAB to easily create arrays.
Published   December 12, 2015
Views   18K
Top answer
1 of 3
25

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.

2 of 3
11

linspace and the colon operator do different things.

linspace creates a vector of integers 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. I find linspace more convenient when the number of gaps is easy to express, whereas I find the colon operator more convenient when the increment is easy to express.

See this MathWorks technical note for more detail on the algorithm behind the colon operator. For more detail on linspace, you can just type edit linspace to see exactly what it does.

🌐
MathWorks
mathworks.com › matlabcentral › answers › 238150-what-is-the-difference-between-a-vector-and-using-linspace
What is the difference between a vector and using linspace? - MATLAB Answers - MATLAB Central
August 31, 2015 - I generally use linspace when I want to determine the length, and usually use the colon operator when I want integer values in the vector. Personal preference, others likely have different views. Sign in to comment. Sign in to answer this question. Find more on Logical in Help Center and File Exchange ... Find the treasures in MATLAB Central and discover how the community can help you!
🌐
Stack Overflow
stackoverflow.com › questions › 49639949 › what-is-the-difference-between-defining-a-vector-using-linspace-and-defining-a-v
matlab - what is the difference between defining a vector using linspace and defining a vector using steps? - Stack Overflow
The linspace function produces a fixed-length vector (the length being defined by the third input argument, which defaults to 100) whose lower and upper limits are set, respectively, by the first and the second input arguments.
🌐
Unsw
maths.unsw.edu.au › sites › default › files › MatlabSelfPaced › lesson3 › MatlabLesson3_Colon.html
MATLAB Lesson 3 - colon operator
The command linspace(a, b, n) creates n equally spaced points between a and b, including both a and b. ... As both end points are included, this produces the vector with elements 1.0000 1.1111 1.2222 1.3333 1.4444 1.5556 1.6667 1.7778 1.8889 2.0000. If you wanted points with a spacing of 0.1, ...
🌐
Delft Stack
delftstack.com › home › howto › matlab › how to use linspace in matlab
The linspace() Function in MATLAB | Delft Stack
March 14, 2025 - The linspace() function in MATLAB is a powerful utility for generating linearly spaced vectors. Whether you’re creating simple ranges for calculations, preparing data for plotting, or working with complex numbers, linspace() simplifies the ...
🌐
MathWorks
mathworks.com › videos › linearly-spaced-vectors-97476.html
Linearly Spaced Vectors - MATLAB
When I look at code written by new users, sometimes, I will see code like this: a = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]; There is an easier way: a = 1:13; This three minute video shows how to use commands like linspace and the colon operator to mak
Published   October 26, 2007
🌐
Exponenta
docs.exponenta.ru › matlab › language fundamentals › matrices and arrays
linspace
Если n нуль или отрицательный, linspace возвращает пустую матрицу 1 на 0.