Matlab

What is colon notation in MATLAB and how to use it?

MATLAB is a high-performance computing environment that offers a wide range of operators, commands, and built-in functions to program with it. It was specifically designed to perform array operations. One such array operation is array addressing which can be performed using the colon operator.

This is a detailed guide that will discuss:

What is Colon Notation?

How to Use Colon Notation in MATLAB?

How to Use Colon Notation in Vectors?

How to Use Colon Notation in Matrices?

How to Use Colon Notation in Multidirectional Arrays?

How to Use Colon Notation in for Loop?

Conclusion

What is Colon Notation?

The colon is a useful MATLAB operator. It is denoted by (:) notation. It has many applications in MATLAB such as it is used in vector creation, array addressing, and for loop.

How to Use Colon Notation in MATLAB?

There are various ways to use colon notation in MATLAB such as:

  • It is used to create vectors.
  • It is used to access vector elements
  • It is used to access matrix elements
  • It is used to access multidirectional array elements
  • It is used to specify for loop iterations

How to Use Colon Notation in Vectors?

The colon notation is used in vectors to perform two tasks:

  • It is used to create vectors.
  • It is used to access specified one or more vector elements.

Syntax

The colon notation follows the given syntaxes in MATLAB to work with vectors:

x=i:j

x=i:j:k

x(i:j)

x(:)

Here,

x=i:j creates a row vector containing consecutive elements from i to j such as [i,i+1,i+2,…,j] where i and j are real integers. If one of the element i or j is complex, it will create a vector with a warning and use only the real part of that complex number. It creates an empty vector if i>j.

x=i:j:k creates a row vector containing elements from i to k having linear difference j between the elements such as [i,i+j,i+2*j,i+3*j,i+m*j]. It creates an empty vector if j==0, j>0 and i>k, or j<0 and i<k.

x(i:j) access the elements from the indices i to j of the given vector x such as [x(i),x(i+1),x(i+2),…,x(j)].

x(:) creates a column vector by accessing all elements of the given vector x. If x is already a column vector, it has no effect on it.

Example 1: How to Use Colon Notation to Create a Row Vector in MATLAB?

This example creates a row vector x containing elements ranging from 5 to 15 using the colon notation in MATLAB.

x=5:15

 

Example 2: How to Use Colon Notation to Create a Linearly Spaced Row Vector in MATLAB?

This MATLAB code uses the colon notation to create a row vector containing the elements ranging from 5 to 15 having a linear difference of 2.

x=5:2:15

 

Example 3: How to Use Colon Notation and fi Constructor to Create a Row Vector in MATLAB?

In this MATLAB code, we use the fi constructor to create a linearly spaced row vector that contains elements ranging from 5 to 15 with the linear space of 2 using the colon notation.

a = fi(5);

b = fi(2);

c = fi(15);

a:b:c

Click here to learn about fi constructor in MATLAB.

 

Example 4: How to Access Vector Elements Using Colon Notation in MATLAB?

In the given example, we create a linearly spaced row vector x. After that, we use the colon notation to access the x’s elements placed on the indices ranging from 2 to 5.

x=5:2:15

x(2:5)

 

Example 5: How to Convert a Row Vector into Column Vector Using Colon Notation in MATLAB?

The below-given example creates a linearly spaced row vector x and uses the colon notation to access all elements of x in the form of a column vector.

x=5:2:15

x(:)

 

How to Use Colon Notation in Matrices?

In MATLAB, the colon notation allows us to access matrix elements. It can access one or more matrix elements by specifying their indices.

Syntax

The colon notation uses the following syntaxes in MATLAB to work with matrices:

A(i,:)
A(:,j)
A(i_init:i_max,j_init:j_max)
A(:)

Here.

A(i,:) accesses all elements of the given matrix A placed on all columns of the ith row of A.

A(:,j) accesses all elements of the given matrix A placed on all rows of the jth column of A.

A(i_init:i_max,j_init:j_max) accesses specified rows ranging from i_init to i_max and specified columns ranging from j_init to j_max of the given matrix A.

A(:) accesses all elements of matrix A and reshapes it into a single-column vector.

 

Example 1: How to Use Colon Notation to Access All Columns of the Specified Row of a Matrix?

In this example, we use colon notation to access all columns of the 4th row of the given matrix A. As a result, we obtain a row vector B.

A = magic(7)

B = A(4,:)

 

Example 2: How to Use Colon Notation to Access All Rows of the Specified Column of a Matrix?

In this MATLAB code, we access all rows of the 6th column of the given matrix A using the colon notation. As a result, we obtain a column vector B.

A = magic(7);

B = A(:,6)

 

Example 3: How to Use Colon Notation to Access Specific Rows and Columns of a Matrix?

The below given MATLAB code accesses the rows ranging from 2 to 5 and columns ranging from 3 to 7 of the given matrix A using the colon notation. As a result, we obtain a matrix B having size 4-by-5 which is a submatrix of matrix A.

A = magic(7);

B = A(2:5,3:7)

 

Example 4: How to Use Colon Notation to Reshape a Matrix to a Single Column Vector?

This example uses the colon notation to convert a given matrix A into a single column vector B having all elements of the matrix A.

A = magic(7);

B = A(:)

 

How to Use Colon Notation in Multidirectional Arrays?

The colon notation can be implemented in multidirectional arrays to access their specific rows, columns, or dimensions.

Syntax

The colon notation can be implemented in the given ways in MATLAB while working with multidirectional arrays:

A(:,j,k)
A(:,:,k)
A(i_init:i_max,j_init:j_max,k_init:k_max)
A(i,j,k,:)A(:,:)
A(:)

Here,

A(:,j,k) accesses all rows of the specific column j and dimension k of the multidirectional array A. In the same way, we can also access all columns of the specific row and dimension, and all dimensions of the specific row and column of the multidirectional array A.

A(:,:,k) accesses all rows and columns of the specific dimension k of multidirectional array A. In the same way, we can also access all other dimensions of the specific row or column.

A(i_init:i_max,j_init:j_max,k_init:k_max) accesses specified rows ranging from i_init to i_max, specified columns ranging from j_init to j_max, and specified dimensions ranging from k_init to k_max of the given multidirectional array A.

A(i,j,k,:) accesses all elements of the four-dimensional array A for the specific values of i, j, and k. As a result, it will create a multidirectional array containing elements A(i,j,k,1), A(i,j,k,2), and so on.

A(:,:) reshapes a multidirectional array A into a two-dimensional array or a matrix containing all elements of A. If A is already a matrix, it has no effect on it.

A(:) reshapes the multidirectional array A into a single-column vector containing all elements of all dimensions of A.

 

Example 1: How to Use Colon Notation to Access All Rows of the Specified Column and Dimension of a Multidirectional Array?

In this MATLAB code, we use colon notation to access all rows of column 2 and the first dimension of the given three-dimensional array A. As a result, we obtain a column vector B.

A = randi(100,2,5,2)

B = A(:,2,1)

 

Example 2 How to Use Colon Notation to Access All Rows and Columns of the Specified Dimension of a Multidirectional Array?

This MATLAB code accesses all rows and columns of the 2nd dimension of the given three-dimensional array A. As a result, it creates a matrix B having dimension 2-by-5.

A = randi(100,2,5,2)

B = A(:,:,2)

 

Example 3: How to Use Colon Notation to Access Specific Rows, Columns, and Dimensions of a Multidirectional Array?

In this example, we use the colon notation to access the rows ranging from 3 to 5, columns ranging from 2 to 4, and dimensions running from 3 to 6 of the given multidirectional array A. As a result, we obtain a multidirectional array B that is a subarray of A.

A = randi(100,10,5,10);

B = A(3:5,2:4,3:6)

 

Example 4: How to Use Colon Notation to Access All Fourth Dimensions of the Specific Row, Column, and Third Dimension of a Four-Dimensional Array?

The below given MATLAB code accesses all 4th dimensions corresponding to the 5th row, 2nd column, and 7th dimension of the four-dimensional array A. As a result, it creates a four-dimensional array B that is a subarray of A.

A = randi(100,10,5,10,4);

B = A(5,2,7,:)

 

Example 5: How to Use Colon Notation to Reshape a Multidirectional Array to a Matrix?

This example implements the colon notation to reshape a four-dimensional array A into a matrix B containing all elements of A.

A = randi(100,10,2,4,2);

B = A(:,:)

 

Example 6: How to Use Colon Notation to Reshape a Multidirectional Array to a Single Column Vector?

This MATLAB code implements colon notation to reshape a three-dimensional array A to a single-column vector B containing all elements of A.

A = randi(100,10,5,10);

B = A(:)

 

How to Use Colon Notation in for Loop?

Another use of colon notation in MATLAB is to specify for loop iterations. As we know the for loop in MATLAB operates for specified iterations by incrementing or decrementing the initial iteration till the last specified iteration, these iterations can be incremented or decremented from first to last using the colon notation.

Syntax

The colon notation can be implemented in the for loop through the given syntax:

for variable = startValue : increment : endValue

% Pu
t code here

end

Here,

The colon notation defines a linear spaced vector of iterations ranging from startValue to endValue incremented by the value increment.

 

Example: How to Use Colon Notation to Specify for Loop Iterations?

This example uses the for-loop to create a column vector containing odd numbers lying between 1 and 20. The for-loop iterations are defined using the colon notation starting from 1 and ending at 20 having a difference of 2 between two consecutive iterations.

for i=1:2:20

fprintf("%d\n",i)

end

 

Conclusion

The colon (:) is a useful MATLAB operator widely used to perform many tasks. It allows us to create a vector, access vector, matrix, and multidirectional array elements, and to specify for loop iterations in different ways. This tutorial has explained what is a colon operator and how to use it in MATLAB. It has also presented all possible ways to implement the colon operator using their corresponding examples.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.