MATLAB Colon Operator in Matrix Indexing Expressions

The colon is a shorthand for “all indices” when it is used in matrix indexing expressions.


Create a matrix using the following code:

A = [1, 5, 2; 3, 6, 4];

Matrix indexing

The colon operator : is used in subscript indexing and linear indexing to denote all possible indices (Table 1).

% Elements in 'all' rows but in the first column of A
firstCol = A(:,1);
Table 1. Common uses of the colon operator in matrix indexing.
Use Meaning
A(i,:) Elements in the \(i\)-th row of A
A(:,j) Elements in the \(j\)-th column of A
A(:,:,k) Elements in the \(k\)-th plane of a 3D matrix A
A(:) All elements in A, regarded as a single column
A(j:k) Elements in A whose linear index is in the range \([j,k]\)


You need to know subscript indexing and linear indexing to fully understand these examples.

% Elements in the first row of A
resultExample1 = A(1,:);
% Elements in the last two columns
resultExample2 = A(:,2:3);
% All elements as a column vector
resultExample3 = A(:);

Further reading

I recommend the following books to learn more on the colon operator in MATLAB:

  1. MATLAB: A Practical Introduction to Programming and Problem Solving (4th Edition)

    This book was the winner of a Textbook Excellence Award. The colon operator is explained clearly in the second chapter.

  2. MATLAB for Engineers (5th Edition)

    This excellent book is especially oriented to engineers and scientists who want to learn MATLAB programming. The fourth chapter is completely dedicated to matrix manipulation.

I also recommend the following page in the MATLAB help documentation, and the other tutorials in this series, but especially this tutorial.

doc :;

Source code

I hope you have learned how to use the colon operator for matrix indexing purposes. The source code developed in this tutorial is available at this page.


Your friends may need this knowledge to complete a task. Share this tutorial.