Description

x = j:i:k creates
a regularly-spaced vector x using i as
the increment between elements. The vector elements are roughly equal
to [j,j+i,j+2*i,...,j+m*i] where m = fix((k-j)/i).
However, if i is not an integer, then floating
point arithmetic plays a role in determining whether colon includes
the endpoint k in the vector, since k might
not be exactly equal to j+m*i.
If you specify nonscalar arrays, then MATLAB interprets j:i:k as j(1):i(1):k(1).

x = colon(j,k) and x = colon(j,i,k) are
alternate ways to execute the commands j:k and j:i:k,
but are rarely used. These syntaxes enable operator overloading for
classes.

A(:,n), A(m,:), A(:),
and A(j:k) are common indexing expressions for
a matrix A that contain a colon. When you use a
colon as a subscript in an indexing expression, such as A(:,n),
it acts as shorthand to include all subscripts
in a particular array dimension. It is also common to create a vector
with a colon for the purposes of indexing, such as A(j:k).
Some indexing expressions combine both uses of the colon, as in A(:,j:k).

Common indexing expressions that contain a colon are:

A(:,n) is the nth
column of matrix A.

A(m,:) is the mth
row of matrix A.

A(:,:,p) is the pth
page of three-dimensional array A.

A(:) reshapes all elements of A into
a single column vector. This has no effect if A is
already a column vector.

A(:,:) reshapes all elements of A into
a two-dimensional matrix. This has no effect if A is
already a matrix or vector.

A(j:k) uses the vector j:k to
index into A and is therefore equivalent to the
vector [A(j), A(j+1), ..., A(k)].

A(:,j:k) includes all subscripts
in the first dimension but uses the vector j:k to
index in the second dimension. This returns a matrix with columns [A(:,j),
A(:,j+1), ..., A(:,k)].

k — Ending vector valuescalar

Ending vector value, specified as a real numeric scalar. k is
the last value in the vector only when the increment lines up to exactly
land on k. For example, the vector 0:5 includes
5 as the last value, but 0:0.3:1 does not include
the value 1 as the last value since the increment does not line up
with the endpoint.

Output Arguments

x — Regularly-spaced vectorrow vector

Regularly-spaced vector, returned as a row vector. If j
> k, then x = j:k is an empty matrix.
More generally, the syntax x = j:i:k returns an
empty matrix when:

i, j, or k is
an empty input

i == 0

i > 0 and j > k

i < 0 and j < k

Tips

The for reference
page has a description of how to use : in the context
of loop statements.

linspace is
similar to the colon operator :, but it gives direct
control over the number of points and always includes the endpoints.
The sibling function logspace generates
logarithmically spaced values.

When you create a vector to index into a cell array
or structure array (such as cellName{:} or structName(:).fieldName), MATLAB returns
multiple outputs in a comma-separated list. For more information,
see How to Use the Comma-Separated Lists.

Extended Capabilities

Tall ArraysCalculate with arrays that have more rows than fit in memory.