linspace — Generate linearly spaced row vectors in MATLAB and RunMat.
linspace(a, b, n) returns n linearly spaced points between a and b, inclusive. Default point count and row-vector output behavior follow MATLAB semantics.
Syntax
x = linspace(start, stop)
x = linspace(start, stop, n)Inputs
start- Type
NumericScalar- Required
- Yes
- Description
- Starting value.
stop- Type
NumericScalar- Required
- Yes
- Description
- Ending value.
n- Type
NumericScalar- Required
- No
- Default
100- Description
- Number of points.
Returns
x- Type
NumericArray- Description
- Row vector of linearly spaced values.
Errors
- When
- More than three input arguments are provided.
- Message
linspace: expected at most three input arguments
- When
- The count argument is not a numeric scalar value.
- Message
linspace: number of points must be a scalar
- When
- The count argument is infinite.
- Message
linspace: number of points must not be infinite
- When
- The count argument exceeds platform limits.
- Message
linspace: number of points is too large for this platform
Behavior
linspace(a, b) is equivalent to linspace(a, b, 100).
linspace(a, b, 0) returns a 1×0 empty row vector whose floating class follows the endpoints.
linspace(a, b, 1) returns b.
linspace(a, b, 2) returns [a b].
Complex end points are supported; the real and imaginary parts are interpolated independently.
Endpoints may be single- or double-precision scalars, 1×1 tensors, or GPU scalars; integer and logical endpoints are rejected.
The third argument may be a floating scalar or any of the eight integer classes. Finite floating counts are floored, non-positive counts produce an empty row vector, and infinite or platform-oversized counts raise an error.
A NaN count produces a 1×1 row vector containing NaN.
Execution and residency
You usually do not need to call gpuArray yourself in RunMat. A resident endpoint selects its exact owning provider; endpoints from different devices are rejected. The runtime prefers that owner's linspace hook for real sequences. If direct generation is unavailable, it generates on the host and restores the result to the same owner when the provider supports the required real or complex class and precision. If an automatically resident endpoint cannot be restored in the required class, the correctly typed result remains on the host; an explicit resident endpoint instead reports an error.
When the provider returns a resident real sequence, RunMat checks its 1×n shape, exact owning device and provider, real storage, and requested single or double precision. With resident endpoints, the result must not share either endpoint's allocation, and their provenance is propagated to it. A malformed provider result or operational hook failure is an error, not a silent host replay; an unavailable hook may use the admitted host-generation-and-restore route.
Examples
Creating five points between zero and one
x = linspace(0, 1, 5)Expected output:
% Row vector with five equally spaced points
x = [0 0.25 0.5 0.75 1]Sampling 100 points when the third argument is omitted
t = linspace(-pi, pi)Expected output:
% t is a 1x100 row vector spanning [-pi, pi]
disp(t(1));
disp(t(end))Constructing complex breakpoints
z = linspace(1+1i, -3+2i, 4)Expected output:
z =
1.0000 + 1.0000i -0.3333 + 1.3333i -1.6667 + 1.6667i -3.0000 + 2.0000iStaying on the GPU automatically
a = gpuArray(0);
b = gpuArray(2*pi);
theta = linspace(a, b, 1024);
wave = sin(theta)Expected output:
% theta and wave remain on the GPU when an acceleration provider is active.
disp(gather(theta(1:5)))Using linspace with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how linspace changes the result.
Run a small linspace example, explain the result, then change one input and compare the output.
FAQ
What is the default number of points?⌄
If you omit the third argument, RunMat uses 100 points, matching MATLAB.
Does linspace include the end points?⌄
Yes. The first element is exactly the start point a and the final element is exactly the end point b.
Can I request zero points?⌄
Yes. Zero and negative counts return a 1×0 empty row vector.
Does the third argument need to be an integer?⌄
No. RunMat floors a finite floating count, accepts exact scalar counts from every integer class, and maps non-positive counts to an empty result. Infinite and platform-oversized counts raise an error.
Does linspace support complex inputs?⌄
Yes. Real and imaginary components are interpolated independently. Mixed real and complex inputs return a complex row vector.
How accurate is the final point?⌄
The implementation explicitly overwrites the final element with b to avoid floating-point drift, matching MATLAB behaviour.
Can I pass GPU scalars?⌄
Yes. RunMat reads each endpoint through its exact owning provider. Real sequences use that provider's linspace hook when available; host-generated real or complex results are restored to the owner when it can represent the output class. Endpoints owned by different devices are rejected.
What precision is used?⌄
Double endpoints produce double output. If either endpoint is single precision, RunMat produces single output; a provider must support that precision to keep a restored result resident.
How does linspace differ from the colon operator?⌄
linspace lets you specify the number of points directly. The colon operator (start:step:end) fixes the spacing instead.
What happens when a equals b?⌄
Every element equals a (and b). For example, linspace(5, 5, 4) returns [5 5 5 5].
What does linspace do in MATLAB?⌄
linspace(a, b, n) generates a row vector of n evenly spaced points between a and b, inclusive. The default value of n is 100.
How is linspace different from the colon operator?⌄
The colon operator a:step:b specifies a step size and may not include the endpoint exactly. linspace(a, b, n) specifies the number of points and always includes both endpoints.
Can I use linspace to create logarithmically spaced points?⌄
Not directly. Use logspace(a, b, n) for logarithmic spacing, which is equivalent to 10.^linspace(a, b, n).
Does linspace work with complex numbers?⌄
Yes. linspace(1+2i, 5+10i, 5) linearly interpolates both the real and imaginary parts independently.
Can I use linspace with GPU arrays in RunMat?⌄
Yes. Pass compatible GPU scalars from one device. RunMat preserves their owning provider and provenance, using direct generation or one host-generation-and-restore fallback when possible.
Related Array functions
Creation
colon · createArray · empty · eye · false · full · inf · int16.empty · int32.empty · int64.empty · int8.empty · logspace · magic · meshgrid · nan · ndgrid · nonzeros · ones · peaks · rand · randi · randn · randperm · range · sparse · spdiags · speye · spones · sprand · true · uint16.empty · uint32.empty · uint64.empty · uint8.empty · zeros
Accumulation
Sorting Sets
argsort · intersect · ismember · ismembertol · issorted · issortedrows · setdiff · setxor · sort · sortrows · union · unique
Shape
blkdiag · cat · circshift · diag · flip · fliplr · flipud · horzcat · ipermute · kron · permute · repelem · repmat · reshape · rot90 · shiftdim · squeeze · toeplitz · tril · triu · vertcat
Combinatorics
Binning
Grouping
Source you can read
Every RunMat function's source is public. Read exactly how linspace is executed, line by line, in Rust.
- View the source for linspace in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.