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
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
start | NumericScalar | Yes | — | Starting value. |
stop | NumericScalar | Yes | — | Ending value. |
n | NumericScalar | No | 100 | Number of points. |
Returns
| Name | Type | Description |
|---|---|---|
x | NumericArray | Row vector of linearly spaced values. |
Errors
| Identifier | When | Message |
|---|---|---|
| — | More than three input arguments are provided. | linspace: expected at most three input arguments |
| — | The count argument is not a numeric scalar value. | linspace: number of points must be a scalar |
| — | The count argument is infinite. | linspace: number of points must not be infinite |
| — | The count argument exceeds platform limits. | linspace: number of points is too large for this platform |
How linspace works
linspace(a, b)is equivalent tolinspace(a, b, 100).linspace(a, b, 0)returns a1×0empty row vector whose floating class follows the endpoints.linspace(a, b, 1)returnsb.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×1tensors, 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
NaNcount produces a1×1row vector containingNaN.
Does RunMat run linspace on the GPU?
GPU endpoints are downloaded only through their exact owner. For real output, RunMat first offers generation to that owner's linspace hook and validates the returned handle's shape, device, owner, precision, and non-aliasing. Otherwise it generates on the host and restores the result to the source owner when representable, preserving explicit or automatic provenance; complex output follows the same gather-and-restore rule.
GPU memory 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; otherwise the correctly typed result remains on the host.
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 · logspace · magic · meshgrid · nan · nchoosek · ndgrid · nonzeros · ones · peaks · perms · rand · randi · randn · randperm · range · sparse · spdiags · speye · spones · sprand · true · zeros
Grouping
accumarray · combinations · discretize · findgroups · groupcounts · grp2idx · splitapply
Sorting Sets
argsort · intersect · ismember · ismembertol · issorted · issortedrows · setdiff · setxor · sort · sortrows · union · unique
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. 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 an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 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.