colon — Generate arithmetic progressions with MATLAB and RunMat colon semantics.

colon(start, stop) and colon(start, step, stop) build row vectors like the MATLAB colon operator. The sequence starts at start, advances by step (defaulting by bound direction), and stops before exceeding stop under MATLAB/RunMat rules.

Syntax

x = colon(start, stop)
x = colon(start, step, stop)

Inputs

NameTypeRequiredDefaultDescription
startAnyYesStart scalar value.
stopAnyYesStop scalar value (implicit step = 1).
stepAnyYesNon-zero increment.
stopAnyYesStop scalar value.

Returns

NameTypeDescription
xAnyArithmetic progression row vector (numeric or character).

Errors

IdentifierWhenMessage
More than three input arguments are provided.colon: expected two or three input arguments
RunMat:IndexStepZeroThe explicit increment is zero.colon: increment must be nonzero
At least one input is not scalar.colon: expected scalar input

How colon works

  • Inputs must be real scalars (numeric, logical, scalar tensors, or single-character arrays). Imaginary parts must be zero.
  • colon(start, stop) picks an increment of +1 when stop ≥ start, otherwise -1.
  • colon(start, step, stop) uses the supplied increment. A zero increment raises an error.
  • When the endpoints are character scalars, the result is a row vector of characters; otherwise a double-precision row vector. Empty progressions are 1×0.
  • stop is included only when it lies on the arithmetic progression; otherwise the sequence stops at the last admissible value before overshooting.
  • Arguments may be gpuArray scalars; the result stays on the GPU when a provider is active.
  • Floating-point tolerance follows MATLAB’s rules, so values that should land on stop are preserved even when rounding noise accumulates.

GPU memory and residency

RunMat automatically keeps the output on the GPU when any input scalar already resides there and an acceleration provider is active. Providers that implement the linspace hook (such as the wgpu backend) generate the progression entirely on device. Other providers still return a GPU tensor by uploading the host-generated vector, so downstream kernels can fuse without an extra gather.

Examples

Generating consecutive integers

x = colon(1, 5)

Expected output:

x = [1 2 3 4 5]

Counting down without specifying a step

y = colon(5, 1)

Expected output:

y = [5 4 3 2 1]

Using a custom increment

z = colon(0, 0.5, 2)

Expected output:

z = [0 0.5 1.0 1.5 2.0]

Stopping before overshooting the end point

vals = colon(0, 2, 5)

Expected output:

vals = [0 2 4]

Working with fractional radians

theta = colon(-pi, pi/4, pi/2)

Expected output:

theta = [-3.1416 -2.3562 -1.5708 -0.7854 0.0000 0.7854 1.5708]

Keeping sequences on the GPU

g = gpuArray(0);
h = colon(g, 0.25, 1);
result = gather(h)

Expected output:

result = [0 0.25 0.5 0.75 1.0]

Building character ranges

letters = colon('a', 'f')
odds    = colon('a', 2, 'g')

Expected output:

letters = 'abcdef';
odds    = 'aceg'

Using colon with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how colon changes the result.

Run a small colon example, explain the result, then change one input and compare the output.

FAQ

What happens when start == stop?

The output is a single-element vector containing start. With two arguments the implicit step is +1, so the result is [start].

Why is stop sometimes missing from the result?

stop is included only when it aligns with the arithmetic progression. For example, colon(0, 2, 5) produces [0 2 4] because 6 would overshoot the upper bound.

Can I use zero or complex increments?

No. The increment must be a finite, non-zero real scalar. Supplying 0 or a value with a non-zero imaginary part raises an error.

Can I generate character sequences?

Yes. When both start and stop are single-character arrays, colon returns a character row vector. Step values can still be numeric (for example, colon('a', 2, 'g') produces 'aceg').

Does the function accept logical inputs?

Yes. Logical scalars are promoted to doubles (true → 1, false → 0) before building the sequence.

How does this differ from linspace?

linspace(start, stop, n) lets you pick the number of points directly, whereas colon fixes the increment (start:step:stop). When stop is not exactly reachable, colon stops short instead of nudging the final value.

Creation

eye · false · fill · inf · linspace · 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 colon 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.