EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Linspace MATLAB
 

Linspace MATLAB

Priya Pedamkar
Article byPriya Pedamkar

Updated March 24, 2023

Linspace MATLAB

 

 

Introduction to Linspace MATLAB

MATLAB is a technical computing language. MATLAB gets its popularity from providing an easy environment for performing and integrating computing tasks, visualizing & programming.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Uses of MATLAB include (but not limited to)

  • Computation
  • Simulation
  • Modeling
  • Data analytics (Analysing and Visualizing data)
  • Prototyping
  • Application development
  • Engineering & Scientific graphics

Linspace Function in MATLAB

In this article, we will understand a very useful function of MATLAB called ‘linspace’. This function will generate a vector of values linearly spaced between two endpoints. It will need two inputs for the endpoints and an optional input to specify the number of points to include in the two endpoints.

Syntax of linspace function:

X = linspace(a1, a2)

Now let us understand this one by one

1. X=linspace(a1,a2)

This function will return a row of a vector of 100(default) linearly spaced points between a1 and a2

  • a1 and a2 can be real or complex
  • a2 can be either larger or smaller than a1
  • If a2 is smaller than a1 then the vector contains descending values

Here is an example to understand this:

Example #1

X = linspace(-1, 1)

It will generate a vector of 100 evenly spaced vectors for the interval [-1, 1]

Output:

 generate a vector

Example #2

X = linspace(2, 3)

It will generate a vector of 100 evenly spaced vectors for the interval [2,3]

Output:

MATLAB console

Example #3

X = linspace(2, 1)

Here a2 is smaller than a1, it will generate a vector of 100 evenly spaced vectors for the interval [2,1] in descending order

Output:

spaced vectors for the interval

2. X=linspace(a1,a2,n)

This function will return a row of a vector of “n” points as specified in input for linearly spaced points between a1 and a2. This function gives control of the number of points and will always include the endpoints specified in the input as well.

The spacing between the points is (a2-a1)/(n-1).

  • If n is 1, the function will return a2 as output
  • If n is zero or negative, the function will return 1by0 empty matrix

Here is an example to understand this:

Example #1

X = linspace(-1, 1, 7 )

It will generate a vector of 7 evenly spaced vectors for the interval [-1, 1]

Output:

Linspace matlab

Example #2

X = linspace(2,3,5)

It will generate a vector of 5 evenly spaced vectors for the interval [2,3]

Output:

Linspace matlab

Example #3

X = linspace(2, 3, 1)

Here n = 1, so the function will return a2 input parameter

Output:

Linspace matlab

Example #4

X = linspace (5, 1, 0)

Here n = 0, so function will return 1X0 empty double row vector

Output:

Linspace matlab

Vector of evenly spaced Complex numbers

X = linspace(2+2i, 3+3i)

Here a1 and a2 are complex numbers, it will generate a vector of complex numbers for 100 evenly spaced points for the interval [2+21, 3+3i]

Output:

Linspace matlab

X= linspace(1+1i, 5+5i, 4)

It will generate a vector of complex numbers with 4 evenly spaced point for the interval [1+1i, 5+5i]

Output:

evenly spaced point

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. The values will be in the range of start and end values passed. So, the linspace function will help us in creating an instantiated matrix or array.

Recommended Articles

This is a guide to Linspace MATLAB. Here we discuss the introduction, Linspace Function in MATLAB and Vector of evenly spaced Complex numbers with examples and outputs. You can also go through our other suggested articles to learn more–

  1. Mean Function in Matlab
  2. Matlab Inverse Function
  3. Swift Operators
  4. Loops in Matlab
  5. Reshape in Matlab | Working | Examples
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW