RunMat
GitHub

linspace — Generate linearly spaced row vectors that match MATLAB behaviour.

linspace(a, b, n) returns n linearly spaced points between the start point a and the end point b, inclusive. When n is omitted, MATLAB (and RunMat) default to 100 points. The result is always a row vector.

How linspace works in RunMat

  • 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.

How linspace runs 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)))

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.

These functions work well alongside linspace. Each page has runnable examples you can try in the browser.

zeros, ones, rand, gpuArray, gather, colon, eye, false, fill, logspace, magic, meshgrid, randi, randn, randperm, range, true

Open-source implementation

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

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.

  • Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
  • Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
  • A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.

Getting started · Benchmarks · Pricing

Try RunMat — free, no sign-up

Start running MATLAB code immediately in your browser.