RunMat
GitHub

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

NameTypeRequiredDefaultDescription
startNumericScalarYesStarting value.
stopNumericScalarYesEnding value.
nIntegerScalarNo100Number of points.

Returns

NameTypeDescription
xNumericArrayRow vector of linearly spaced values.

Errors

IdentifierWhenMessage
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 not an integer value.linspace: number of points must be an integer

How linspace works

  • linspace(a, b) is equivalent to linspace(a, b, 100).
  • linspace(a, b, 0) returns a 1×0 empty double row vector.
  • 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.
  • Inputs may be scalars, 1×1 tensors, or GPU scalars (gpuArray handles).
  • The third argument must be a finite, non-negative integer.

Does RunMat run linspace on the GPU?

If either endpoint is already resident on the GPU, RunMat keeps the result on the device whenever the active acceleration provider supports uploading host buffers. Providers may additionally implement the optional linspace hook to generate the sequence entirely on device. Until then, RunMat generates the sequence on the host and performs a single upload so downstream kernels can consume the GPU data without extra gathers.

GPU memory and residency

You usually do not need to call gpuArray yourself in RunMat. When either endpoint already resides on the GPU, RunMat keeps the generated sequence on the device to preserve residency for downstream operations. When the active provider implements the dedicated linspace hook (the WGPU backend does), the sequence is generated directly on-device. If no acceleration provider is registered, or if a provider lacks the hook, the runtime emits the values on the host and uploads them once, preserving correctness with a modest transfer.

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.0000i

Staying 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. linspace(a, b, 0) returns a 1×0 empty row vector.

Does the third argument need to be an integer?

Yes. Non-integer or negative counts raise an error, just like MATLAB.

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. GPU endpoints are gathered once to compute the sequence. The result stays on the GPU as long as a provider is available and the endpoints are real.

What precision is used?

RunMat mirrors MATLAB and returns double-precision vectors. Providers may choose to specialise future hooks for other precisions.

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 GPU scalars to linspace and the result will be a GPU-resident array, avoiding unnecessary host-device transfers.

Creation

colon · eye · false · fill · inf · logspace · magic · meshgrid · nan · ones · peaks · rand · randi · randn · randperm · range · true · zeros

Sorting Sets

argsort · intersect · ismember · issorted · setdiff · sort · sortrows · union · unique

Shape

cat · circshift · diag · flip · fliplr · flipud · horzcat · ipermute · kron · permute · repelem · repmat · reshape · rot90 · squeeze · tril · triu · vertcat

Indexing

find · ind2sub · sub2ind

Introspection

isempty · ismatrix · isscalar · isvector · length · ndims · numel · size

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how linspace is executed, line by line, in Rust.

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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.