MATLAB - Array Indexing



Array addressing refers to accessing the elements of an array. Matlab comes with many ways you can access an array. We can make use of indexing, slicing and logical indexing to get the elements from the array.

Array Indexing

A variable is an array in matlab with many numbers in it. To access any element from an array you have to make use of indexing.

To understand in more detail let us create a matrix of size 4x3.

Example

A = [1 2 3; 4 5 6; 7 8 9; 10 11 12]

On execution in Matlab we get the following output −

A =

   1     2     3
   4     5     6
   7     8     9
   10    11    12 

The elements from the array can be fetched −

  • By giving the row and column subscripts
  • By giving the single subscript.
  • By using colon operators with start:end range.

By Giving the Row and Column Subscripts

To get the element from the array, give the row number and column number.

For example I want the element 8, which is located in the 3rd row and 2nd column −

A = [1 2 3; 4 5 6; 7 8 9; 10 11 12]
A(3,2)

When you execute in matlab you will get −

A =

   1     2     3
   4     5     6
   7     8     9
   10    11    12

ans = 8

By Giving the Single Subscript

So from the matrix below you want to fetch the element 5, you can do so by giving a single subscript.

A =

   1     2     3
   4     5     6
   7     8     9
   10    11    12

The subscript to fetch 5 is 6.

So A(6) will give you the element 5. Let us check the same in matlab.

A = [1 2 3; 4 5 6; 7 8 9; 10 11 12]
A(6)
A =

   1     2     3
   4     5     6
   7     8     9
   10    11    12

ans = 5

By Using Colon Operators with start:end Range

The colon is the most commonly used operator in matlab, it is denoted as (:). It is used to get subscript arrays, vectors and also used in for loops.

Here are different results that you will get when you use colon(:) to get elements from a matrix.

Syntax Description
A(:, nth) This will display the nth column of the matrix A.
A(m, : ) This will display the mth row of the matrix A.
A(:) This will give you all elements of the matrix A in a single column vector.
A(i:j) This will give you a vector array that will have elements as shown below [A(i), A(i+1) . Aj]
A(:, i:j) This will give you all the elements from the first dimension, the second dimension will be based on i:j values.

Let us check a few examples for a given matrix A.

Example 1

This example will fetch us the third columns from 4x3 matrix as shown below −

A = [1 2 3; 4 5 6; 7 8 9; 10 11 12]
A(:, 3)

On execution in matlab the output is −

A =

   1     2     3
   4     5     6
   7     8     9
   10    11    12

ans =

    3
    6
    9
   12

Example 2

This example will get the 1st row of the matrix A as shown below −

A = [1 2 3; 4 5 6; 7 8 9; 10 11 12]
A(1,:)

On execution in Matlab the output is as follows −

A =
    1     2     3
    4     5     6
    7     8     9
   10    11    12
   
ans =

   1     2     3

Example 3

Let us display all the elements in the matrix A as a single column vector as shown below.

A = [1 2 3; 4 5 6; 7 8 9; 10 11 12]
A(:)

The output on execution is as follows −

A =
    1     2     3
    4     5     6
    7     8     9
   10    11    12

ans =
    1
    4
    7
   10
    2
    5
    8
   11
    3
    6
    9
   12

Example 4

Let us get elements from the matrix in order as i, i+1 etc. from the range given. Suppose you want the elements from 2 to 8, A(2:8) will return a row vector as shown below.

A = [1 2 3; 4 5 6; 7 8 9; 10 11 12]
A(2:8)

On execution the output is as follows −

A =
    1     2     3
    4     5     6
    7     8     9
   10    11    12

ans =

   4     7    10     2     5     8    11

Example 5

Let us try another example where we need all the elements from the rows and range of elements from the second dimension.

A = [1 2 3; 4 5 6; 7 8 9; 10 11 12]
A(:, 2:3)

On execution the output is as follows −

A =

    1     2     3
    4     5     6
    7     8     9
   10    11    12

ans =

    2     3
    5     6
    8     9
   11    12
Advertisements