linspace

Generate linearly spaced vector

The linspace function generates linearly spaced vectors. It is similar to the colon operator ":" but gives direct control over the number of points and always includes the endpoints.

Syntax

Description

example

y = linspace(x1,x2) returns a row vector with 100 linearly spaced points in the interval [x1,x2].

example

y = linspace(x1,x2,N) returns N linearly spaced points.

Examples

expand all

Create Vector of Evenly Spaced Real Numbers

Create a vector of 100 evenly spaced points in the interval [-5,5].

y = linspace(-5,5);

Create Vector with Specified Number of Values

Create a vector of 7 evenly spaced points in the interval [-5,5].

y1 = linspace(-5,5,7)
y1 =

   -5.0000   -3.3333   -1.6667         0    1.6667    3.3333    5.0000

Create Vector of Evenly Spaced Complex Numbers

Create a vector of complex numbers with 8 evenly spaced points between 1+2i and 10+10i.

y = linspace(1+2i,10+10i,8)
y =

  Columns 1 through 5

   1.0000 + 2.0000i   2.2857 + 3.1429i   3.5714 + 4.2857i   4.8571 + 5.4286i   6.1429 + 6.5714i

  Columns 6 through 8

   7.4286 + 7.7143i   8.7143 + 8.8571i  10.0000 +10.0000i

Input Arguments

expand all

x1,x2 — Point intervalpair of scalars

Point interval, specified as a pair of scalars. x1 and x2 define the interval over which linspace generates linearly spaced points (including the endpoints). x2 can be either larger or smaller than x1. If x2 is smaller than x1, then the vector contains descending values.

Data Types: double | single | datetime | duration

Complex Number Support: Yes

N — Number of points100 (default) | real numeric scalar

Number of points, specified as a real numeric scalar.

For best results, specify N as a positive integer to indicate the number of linearly spaced points in the interval [x1,x2]. Otherwise:

  • If N is 1, then linspace returns x2.

  • If N is not an integer, then linspace uses a number of points equal to floor(double(N)).

  • If N rounds to zero or is negative, then linspace returns an empty 1-by-0 matrix.

Output Arguments

expand all

y — Vector of linearly spaced pointsvector

Vector of linearly spaced points, returned as a vector. y contains N linearly spaced points in the interval [x1,x2], including the endpoints, with the default number of points being 100.

linspace(x1,x2,N) uses a step size of (x2-x1)/(N-1).

See Also

|

Was this topic helpful?