In this tutorial we learn how to index a 2D Matrix or array in MATLAB. Properly indexing arrays is necessary to use MATLAB effectively. This tutorial will teach you various ways to properly access modify or use values stored in a 2D array.
Before discussing indexing, it is important to review how data is organized in an array. 2D arrays are organized in two-dimensions (duh!) i.e. rows and columns. The figure below shows an array with four rows and four columns. Row no increases from top to bottom and the column no increases from left to right. Each “cell” has an address / pair of indices that refer to the column no and row no in which it is located. As an example the green cell in the figure is located in Row 3 and Col 2. Total elements in the 2D array is a product of total number of row and columns in the array (4*4 = 16 elements in our case).
Lets define a 2D matrix of size 4×4 called “A”.
>> A = [1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16] A = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Now lets look at how values in A are stored in the memory. In the following figure, the memory is shown on the right side.
The elements of A are stored at consecutive locations with the four elements of column 1 stored first (in order), followed by elements of column 2 and so on. The address is increasing from top to bottom. Now lets look at two main ways of indexing 2D arrays.
Using a single Index
In this method a single index is used to access elements of the array. The syntax is
element_no is the offset from the base memory location (where array starts). This method is similar to indexing a vector. Please read previous article titled How to Index a Vector in MATLAB for details on various methods for indexing vectors.
A(9) should give us the 9th element i.e. 3
>> A(9) ans = 3
A([4 12]) returns a vector with 4th and 12th element.
>> A([4 12]) ans = 13 15
A([4 20]) returns an error “Index exceeds matrix dimensions” since 20 is not a valid index. Our matrix has 4×4 = 16 elements.
>> A([4 20]) Index exceeds matrix dimensions.
To get the first row of the array, we can specify a range of with increment 4 i.e. A([1:4:end])
>> A([1:4:end]) ans = 1 2 3 4
This method always returns a row vector instead of a properly shaped sub-array. This method doesn’t work well because most of the time we require whole sub-arrays or rows / columns from indexing 2D arrays. The far easier method of indexing is by using a pair of indices i.e. rows and cols.
Using a pair of Indices
The syntax is
There are various methods of specifying rows and cols. rows and cols can be
- Single value
- Colon (:)
- Any combination of above methods
Lets first see a couple of examples where both rows and cols are single values. A(2,4) returns the element in the 2nd row and 4th column i.e. 8.
>> A(2,4) ans = 8
A(4,4) returns the element in the 4th row and 4th column i.e. 16.
>> A(4,4) ans = 16
In both examples MATLAB returns a single value since we are asking MATLAB for an element located at a particular row and col.
Both row and col should be valid, otherwise MATLAB returns an error
>> A(5,4) Index exceeds matrix dimensions.
>> A(3,0) Subscript indices must either be real positive integers or logicals.
Now lets look at few examples where one or both rows and cols are vectors. A(3,[1 2 3]) returns the elements in the 3rd row and 1st, 2nd and 3rd column. These elements are returned in the form of vector with same shape as the shape of the specified rows and cols. In the example A(3,[1 2 3]), this shape will be 1×3 since we gave one row and three cols while indexing.
>> A(3,[1 2 3]) ans = 9 10 11
A([end 2],[1 3]) returns the elements in last row + 2nd row and 1st col + 3rd col. Both rows and cols are vectors in this example. The returned vector has size 2×2. 1st and 3rd elements of row 4 form the first row, where as the 1st and 3rd elements of row 2 form the 2nd row of the output. The order of the elements in the output depends on the order of indices in rows and cols.
>> A([end 2],[1 3]) ans = 13 15 5 7
Using : in place of rows or cols selects all rows or cols. A(:,[1 2 3]) returns elements in all the rows and 1st, 2nd, 3rd col.
>> A(:,[1 2 3]) ans = 1 2 3 5 6 7 9 10 11 13 14 15
A([4 end-2],:) returns 4th, 2nd rows and all cols.
>> A([4 end-2],:) ans = 13 14 15 16 5 6 7 8
To change the values of selected elements of an array, you can use the any of the indexing methods and then assign new value(s). You can assign a single value to all elements. Lets see an example.
>> A([1 2],:) = 10 A = 10 10 10 10 10 10 10 10 9 10 11 12 13 14 15 16
You can assign a separate values to each element by specifying an array of equal shape with new values on right side.
>> A([1 2],:) = [1 2 3 4;5 6 7 8] A = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
If the shape of array is not the same as shape of left side then MATLAB returns an error
>> A([1 2],:) = [1 2 3;5 6 7] Subscripted assignment dimension mismatch.
One or both of rows and cols can be a range. A(1:2,3:4) returns elements of first two rows and last two cols in a 2×2 array.
>> A(1:2,3:4) ans = 3 4 7 8
You can use a mix of any of the four methods of specifying rows and cols. The following example uses a range for rows and a vector for cols.
>> A([1:2],[1 2]) ans = 1 2 5 6
The following example indexes a single row and a range of cols.
>> A(3,3:-1:1) ans = 11 10 9
The rows and cols don’t need to be constants. They can themselves be variables.
>> rowNo = 1:2; colNo = [2 4]; A(rowNo,colNo) ans = 2 4 6 8
>> rowNo = [1 2]; colNo = 3; A(rowNo,colNo) ans = 3 7
We have discussed various ways to index a 2D matrix in MATLAB by using rows and cols. Now lets discuss a different type of indexing known as logical indexing. In logical indexing, instead of using rows and cols, we provide a logical expression. As an example, to get all the elements of “A” that are greater than 8, we can write
>> A(A>8) ans = 9 13 10 14 11 15 12 16
MATLAB reads the elements of the array from memory from top to bottom and returns a column vector with all the elements than are greater than 8.
To replace all the elements of “A” that are less or equal to 5, we can write
>> A(A<=5) = 10 A = 10 10 10 10 10 6 7 8 9 10 11 12 13 14 15 16