MATLAB Colon Notation - MATLAB

What is MATLAB Colon Notation?

The colon(:) is one of the most useful operator in MATLAB. It is used to create vectors, subscript arrays, and specify for iterations.

If you want to create a row vector, containing integers from 1 to 10, you write –

MATLAB executes the statement and returns a row vector containing the integers from 1 to 10 –

If you want to specify an increment value other than one, for example –

MATLAB executes the statement and returns the following result –

Let us take another example –

MATLAB executes the statement and returns the following result –

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


The following table describes its use for this purpose (let us have a matrix A) –

Format Purpose
A(:,j) is the jth column of A.
A(i,:) is the ith row of A.
A(:,:) is the equivalent two-dimensional array. For matrices this is the same as A.
A(j:k) is A(j), A(j+1),...,A(k).
A(:,j:k) is A(:,j), A(:,j+1),...,A(:,k).
A(:,:,k) is the kthpage of three-dimensional array A.
A(i,j,k,:) is a vector in four-dimensional array A. The vector includes A(i,j,k,1), A(i,j,k,2), A(i,j,k,3), and so on.
A(:) is all the elements of A, regarded as a single column. On the left side of an assignment statement, A(:) fills A, preserving its shape from before. In this case, the right side must contain the same number of elements as A.

Example

Create a script file and type the following code in it –

When you run the file, it displays the following result –

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

MATLAB Topics