linspace - Generate linearly spaced vector - MATLAB (original) (raw)
Generate linearly spaced vector
Syntax
Description
y = linspace([x1,x2](#bud27em-x1x2))
returns a row vector of evenly spaced points between x1
and x2
. By default,linspace
generates 100 points.
y = linspace([x1,x2](#bud27em-x1x2),[n](#bud27em-n))
generates n
points. The spacing between the points is(x2-x1)/(n-1)
.
linspace
is 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 function logspace
, which generates logarithmically spaced values.
Examples
Vector of Evenly Spaced Numbers
Create a vector of 100 evenly spaced points in the interval [-5,5]
.
Vector with Specified Number of Values
Create a vector of 7 evenly spaced points in the interval [-5,5]
.
y1 = 1×7
-5.0000 -3.3333 -1.6667 0 1.6667 3.3333 5.0000
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 = 1×8 complex
1.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
x1,x2
— Point interval
pair of scalars
Point interval, specified as a pair of scalars. x1
andx2
define the interval over which linspace
generates points. x2
can be either larger or smaller thanx1
. If x2
is smaller than x1
, then the vector contains descending values.
Data Types: single
| double
| datetime
| duration
Complex Number Support: Yes
n
— Number of points
100 (default) | real numeric scalar | NaN
Number of points, specified as a real numeric scalar orNaN
.
- If
n
is1
,linspace
returnsx2
. - If
n
is zero or negative,linspace
returns an empty 1-by-0 matrix. - If
n
is not an integer,linspace
rounds down and returnsfloor(n)
points. - If
n
isNaN
,linspace
returnsNaN
.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
- Calls to
linspace
with number of points equal toNaN
are not supported.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
GPU Arrays
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
The linspace
function supports GPU array input with these usage notes and limitations:
- To run this function on a GPU and obtain a
gpuArray
output, use any of the following syntaxes:
y = gpuArray.linspace(x1,x2)
y = gpuArray.linspace(x1,x2,n) - Alternatively, you can specify
x1
orx2
as agpuArray
.
For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Distributed Arrays
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
Usage notes and limitations:
- Use
distributed.linspace
to call the distributed version oflinspace
. x1
andx2
must besingle
ordouble
scalars.
For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version History
Introduced before R2006a