Vector creation, array subscripting, and
The colon is one of the most useful operators in MATLAB®.
It can create vectors, subscript arrays, and specify
x = j:k x = j:i:k A(:,n) A(m,:) A(:) A(j:k)
a regularly-spaced vector
the increment between elements. The vector elements are roughly equal
m = fix((k-j)/i).
i is not an integer, then floating
point arithmetic plays a role in determining whether
k in the vector, since
not be exactly equal to
If you specify nonscalar arrays, then MATLAB interprets
x = colon(j,k) and
x = colon(j,i,k) are
alternate ways to execute the commands
but are rarely used. These syntaxes enable operator overloading for
A(j:k) are common indexing expressions for
A that contain a colon. When you use a
colon as a subscript in an indexing expression, such as
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
Some indexing expressions combine both uses of the colon, as in
Common indexing expressions that contain a colon are:
A(:,n) is the
column of matrix
A(m,:) is the
row of matrix
A(:,:,p) is the
page of three-dimensional array
A(:) reshapes all elements of
a single column vector. This has no effect if
already a column vector.
A(:,:) reshapes all elements of
a two-dimensional matrix. This has no effect if
already a matrix or vector.
A(j:k) uses the vector
A and is therefore equivalent to the
[A(j), A(j+1), ..., A(k)].
A(:,j:k) includes all subscripts
in the first dimension but uses the vector
index in the second dimension. This returns a matrix with columns
A(:,j+1), ..., A(:,k)].
Create a unit-spaced vector of numbers between 1 and 10. The colon operator uses a default increment of +1.
x = 1:10
x = 1 2 3 4 5 6 7 8 9 10
Create vectors that increment or decrement by a specified value.
Create a vector whose elements increment by 0.1.
x = 0:0.1:1
x = Columns 1 through 7 0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 Columns 8 through 11 0.7000 0.8000 0.9000 1.0000
Create a vector whose elements decrement by -2.
y = 10:-2:0
y = 10 8 6 4 2 0
Examine several ways to index a matrix using a colon
Create a 3-by-3 matrix. Index the first row.
A = magic(3)
A = 8 1 6 3 5 7 4 9 2
ans = 8 1 6
Index the second and third column.
ans = 1 6 5 7 9 2
Reshape the matrix into a column vector.
ans = 8 3 4 1 5 9 6 7 2
In the context of a
for-loop, the colon specifies the loop iterations.
for-loop that squares a number for values of
n between 1 and 4.
for n = 1:4 n^2 end
ans = 1
ans = 4
ans = 9
ans = 16
j— Starting vector value
Starting vector value, specified as a real numeric scalar. If
< k so that the output vector is not empty, then
the first element in the vector.
x = 0:5
k— Ending vector value
Ending vector value, specified as a real numeric scalar.
the last value in the vector only when the increment lines up to exactly
k. For example, the vector
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.
x = 0:5
i— Increment between vector elements
1(default) | scalar
Increment between vector elements, specified as a real numeric scalar.
x = 0:0.5:5
x— Regularly-spaced vector
Regularly-spaced vector, returned as a row vector. If
> k, then
x = j:k is an empty matrix.
More generally, the syntax
x = j:i:k returns an
empty matrix when:
an empty input
i == 0
i > 0 and
j > k
i < 0 and
j < k
page has a description of how to use
: in the context
of loop statements.
similar to the colon operator
:, but it gives direct
control over the number of points and always includes the endpoints.
The sibling function
logarithmically spaced values.
When you create a vector to index into a cell array
or structure array (such as
), MATLAB returns
multiple outputs in a comma-separated list. For more information,
see How to Use the Comma-Separated Lists.
This function supports tall arrays with some limitations. For more information, see Index and View Tall Array Elements.
Usage notes and limitations:
Complex inputs are not supported.
i cannot have a logical
Vector inputs are not supported.
Inputs must be constants.
Uses single-precision arithmetic to produce single-precision results.