Linspace Function in Matlab – Explanation with Coding Examples


In Matlab coding, sometimes there can be a requirement to create a vector that has specific start and end values. And you need to partition the start and end values of this vector must be a kind of integer. To do it, you could use the linspace command in Matlab. You could create a vector that starts from a number and ends at another number and this gap is divided with an integer equally. Also, you could see the use of linspace in Matlab in this content. Furthermore, the linspance function in Matlab is very useful.

Check: Recommended Books to Learn Matlab®

Use of Linspace Function in Matlab

If we take a look a the syntax of the linspace function in Matlab, we can see we need to enter three values inside this function. The first value is the starting value of the vector. And the second value is the increment value of the vector. This increment value will be added respectively to obtain the equally spaced vectors. The last value is the end value of the linspace vector.

>> v = linspace(1,3,5)
d = 3*linspace(1,3,5)

v =

    1.0000    1.5000    2.0000    2.5000    3.0000

d =

    3.0000    4.5000    6.0000    7.5000    9.0000


As you see above, the use of the linspace command is very simple in Matlab. If you comprehend its logic, it will make your Matlab computations and coding much easier. You can create very big vectors with the linspace command without entering lots of values into vectors. You just need to specify the start, end, and increment value inside the linspace function.

We created two vectors in Matlab® called v and d by using the linspacespace command. The meaning of these linspace vectors in Matlab is like that;

>> v = linspace(1,3,5)
d = 3*linspace(1,3,5)

v =

    1.0000    1.5000    2.0000    2.5000    3.0000

d =

    3.0000    4.5000    6.0000    7.5000    9.0000


As you can see the we created the v vector that is a very straight use of the linspace function in Matlab. It is very basic to use the linspace function in Matlab, that you need to only type numbers by putting commas between them in parentheses after typing linspace. v vector starts with integer 1 and ends with integer 3 then this vector includes 5 elements. So we divide the start and end values according to the element number that you entered into the linspace function in Matlab.

What Happens If We Do not Write an Increment Value?

Above all, we stated that you need to write an increment value inside the linspace command in Matlab. But if we do not write this increment value, the function will divide the vector into 100 elements.

Additional Examples about Linspace Function in Matlab

Check the basic coding example of the linspace command in Matlab to understand the command better.

a = linspace(-1 1 5)

a = 
   -1  -0.5  0   0.5   1

As you see above, we use -1, 1, and 5 values respectively in the linspace function. So, the vector is equally divided by 5 which has the initial value of -1 and the last value of 1. Also, we can use this example in different kinds of mathematical applications.

Besides the linspace function in Matlab, Matlab® provides very useful functions to create vectors and matrices in different forms for different kinds of applications. Check the other useful function of vectors and matrices in Matlab®;

Cumulative Multiplication of Vectors and Matrices in Matlab®

Summation of All Elements of Matrices and Vectors in Matlab®

Editing Matrices and Vectors at Variable Editor in Matlab®(Illustrated Expression)


Above all, as you can see, you could do mathematical calculations with a linspace vector-like in d vector. So, we multiplied the same linspace vector with integer 3 in the d vector.

Linspace is a very useful function that you can easily utilize for different mathematical code applications. In algebra and advanced mathematics topics, you will deal with lots of long arrays and vectors in calculations. So, the linspace command will make your business much easier if you perform your calculations with Matlab tools.

Also, do not forget to leave your comments and questions below about the use of linspace function in Matlab below. 

If you want further coding examples about the linspace function in Matlab, inform us in the comments.

We prepared this article for completely educative and informative purposes. Images used courtesy of Matlab®

Your precious feedbacks are very important to us.

| Recents

| Calculators


Leave a Reply

Your email address will not be published. Required fields are marked *