Matlab

How to Generate Linearly Spaced Vector in MATLAB Using Linspace() Function

A linearly spaced vector is a vector having an equal difference between its all elements. Say we have a vector [1 2 3 4 5] it is an equally spaced vector having an equal difference of 1 between all its entries. MATLAB provides us with two methods that allow us to generate equally spaced vectors. These methods are the colon operator and linspace() function.

In this guide, we will focus on generating equally spaced vectors in MATLAB using the linspace() function.

How to Create Equally Spaced Vectors in MATLAB Using linspace() Function?

The linspace() is a built-in function in MATLAB used for creating vectors having equal differences among all their values. This function accepts 2 mandatory and 1 optional argument. These mandatory arguments are the starting and ending values of the interval in which, we want to create an equally spaced vector. If a number of elements are not specified, the function by default generates a linearly spaced vector having 100 elements.

Syntax

The linspace() function follows a simple syntax that is given below:

x = linspace(start_point, end_point)

x = linspace(start_point, end_point, number_of_elements)

Here:

The function x = linspace(start_point, end_point) accepts 2 mandatory inputs and returns an equally spaced vector having 100 elements.

The function x = linspace(start_point, end_point, number_of_elements) accepts two mandatory and one optional argument and returns a vector having equally spaced values.

Example 1

This example creates an equally spaced vector having 100 elements between 1 and 10 using the linspace() function in MATLAB.

linspace(1, 10)

Example 2

In this example, we generate an evenly spaced vector with 10 elements lying between 1 and 10 using the linspace() function in MATLAB.

linspace(1, 10, 10)

Conclusion

The linspace() function in MATLAB is useful because it allows you to create a linearly spaced vector. This function takes 2 mandatory and 1 optional argument and generates an evenly-spaced vector. If the number of elements in a vector is not specified in the function, this creates an equally spaced vector of 100 elements by default. This tutorial has presented easy guidelines to create equally spaced vectors in MATLAB using the linspace() function.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.