View all functions

CategoryArray: Creation
GPUYes

What does the linspace function do in MATLAB / RunMat?

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 does the linspace function behave in MATLAB / 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.

linspace Function GPU Execution Behaviour

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.

Examples of using the linspace function in MATLAB / RunMat

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 behaviour:

% 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 behaviour:

% theta and wave remain on the GPU when an acceleration provider is active.
disp(gather(theta(1:5)));

GPU residency in RunMat (Do I need gpuArray?)

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.

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

See Also

zeros, ones, rand, gpuArray, gather

Source & Feedback