Then, set x=0, y=10, and n=5 and run the following command in your MATLAB. Let’s say you want 5 values in an interval of. The following command allows you to create a row vector of size ‘n’ with numbers in an interval. Suppose we want an array of size n instead of 100. The above linspace matlab command returns 100 equally spaced points. So, you can also use linspace command to create a vector of numbers in descending order.
Then the above command becomes:Īfter executing this command, you will observe that the numbers are now printed in descending order from 10 to 0.
Let’s swap x and y in example 1 to check what happens. This command will return a row vector M of size 100 with numbers ranging from 0 to 10 in ascending order. Let’s understand this through an example. If x is smaller than y, the vector will be of ascending values whereas if x is greater than y, then it will generate values in descending order. The function will return equally spaced 100 points by default between the numbers x and y. Here x and y are the two numbers specified by the user. It is similar to the colon operator “:”, but gives direct control over the number of points. An easier way to do this is to use Linspace which will give you linearly spaced values starting from a given starting point to the specified ending point. As in the above example, to create an array of size 8, we had to plug in all the numbers from 0 to 7 and this will give us the output of 8 by 1 array. It allows you to bypass the old time-consuming method where we had to plug in all the numbers. Then what is the purpose of the Linspace MATLAB function? This can be done by using following command “A= ”. Suppose you want to create an array A of size 8 with values of 1, 2, 3, 4, 5, 6, and 7. The linspace function allows the programmer to produce an array, matrix, or vector of numbers that are linearly spaced.