Documentation

linspace

Generate linearly spaced vector

Syntax

y = linspace(x1,x2)
y = linspace(x1,x2,n)

Description

example

y = linspace(x1,x2)returns a row vector of 100 evenly spaced points betweenx1andx2.

example

y = linspace(x1,x2,n)generatesnpoints. The spacing between the points is(x2-x1)/(n-1).

linspaceis similar to the colon operator, “:”, but gives direct control over the number of points and always includes the endpoints. “lin” in the name “linspace” refers to generating linearly spaced values as opposed to the sibling functionlogspace, which generates logarithmically spaced values.

Examples

collapse all

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

y = linspace(-5,5);

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

y1 = linspace(-5,5,7)
y1 =1×7-5.0000 -3.3333 -1.6667 0 1.6667 3.3333 5.0000 ⋯

Create a vector of complex numbers with 8 evenly spaced points between1 + 2我and10+10i.

y = linspace(1+2i,10+10i,8)
y =1×8 complex1.0000 + 2.0000i 2.2857 + 3.1429i 3.5714 + 4.2857i 4.8571 + 5.4286i 6.1429 + 6.5714i 7.4286 + 7.7143i 8.7143 + 8.8571i 10.0000 +10.0000i ⋯

Input Arguments

collapse all

Point interval, specified as a pair of numeric scalars.x1andx2define the interval over whichlinspacegenerates points.x1andx2can be real or complex, andx2can be either larger or smaller thanx1. Ifx2is smaller thanx1, then the vector contains descending values.

Data Types:single|double|datetime|duration
Complex Number Support:Yes

Number of points, specified as a real numeric scalar.

  • Ifnis1,linspacereturnsx2.

  • Ifnis zero or negative,linspacereturns an empty 1-by-0 matrix.

  • Ifnis not an integer,linspacerounds down and returnsfloor(n)points.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

See Also

|

Introduced before R2006a

Was this topic helpful?