## Documentation Center |

Create vectors, array subscripting, and `for`-loop
iterators

The colon is one of the most useful operators in MATLAB^{®}.
It can create vectors, subscript arrays, and specify `for` iterations.

The colon operator uses the following rules to create regularly
spaced vectors for scalar values `i`, `j`,
and `k`:

is the same as | |

is the same as |

If you specify nonscalar arrays, MATLAB interprets `j:i:k` as `j(1):i(1):k(1)`.

You can use the colon to create a vector of indices to select rows, columns, or elements of arrays, where:

When you create a vector to index into a cell array or structure
array (such as `cellName{:}` or

Using the colon with integers,

D = 1:4

results in

D = 1 2 3 4

Using two colons to create a vector with arbitrary real increments between the elements,

E = 0:.1:.5

results in

E = 0 0.1000 0.2000 0.3000 0.4000 0.5000

The command

A(:,:,2) = pascal(3)

generates a three-dimensional array whose first page is all zeros.

A(:,:,1) = 0 0 0 0 0 0 0 0 0 A(:,:,2) = 1 1 1 1 2 3 1 3 6

Using a colon with characters to iterate a for-loop,

for x='a':'d',x,end

results in

x = a x = b x = c x = d

Was this topic helpful?