colon

Function form of the colon operator, which builds evenly spaced vectors.

Category: Language Fundamentals > Matrices and Arrays

Syntax

v = colon(a, b)

Same as a:b, steps of 1.

v = colon(a, d, b)

Same as a:d:b, steps of d.

Description

The function exists so that the colon can be passed around as a function handle or used in cellfun and arrayfun calls, where the syntax a:b is not available. The number of elements is floor((b-a)/d) + 1, computed with a small tolerance to absorb rounding in fractional steps.

Input arguments

a
First value.
d
Step size (default 1). May be negative.
b
Limit: the sequence stops at or before it.

Output arguments

v
Row vector. It is empty when the step points away from the limit.

Example

colon(1, 4)
colon(10, -3, 0)
Output
ans = 1×4

     1     2     3     4
ans = 1×4

    10     7     4     1

See also

linspace repmat numel