Detailed Rules for Array Indexing
In MATLAB®, indexing is a fundamental operation for accessing and modifying array elements efficiently. There are three primary approaches to indexing: indexing by position, linear indexing, and logical indexing, as discussed in Array Indexing.
This topic discusses how MATLAB handles indexing into arrays using subscript vectors. MATLAB follows specific indexing rules when you specify indices as subscript vectors. While MATLAB supports subscript matrices and multidimensional arrays as indices, you can convert these indices to subscript vectors depending on your use cases. MATLAB follows additional indexing rules when you specify fewer subscripts than the dimensions of the array you want to access.
Indexing Rules with Subscript Vectors
MATLAB follows these three main indexing rules for subscript vectors:
Given an array
A
and a subscript vectorid
, the resulting arrayA(id)
is the same size asid
, except for the special case whereA
andid
are both vectors. In the latter case,A(id)
has the same number of elements asid
but has the orientation ofA
.Given an array
A
and subscript vectorsid1
andid2
, the resulting arrayA(id1,id2)
is a rectangular submatrix ofA
withnumel(id1)
rows andnumel(id2)
columns. A colon subscript indicates all elements in that dimension. For example,A(id1,:)
has all columns of the rows specified by vectorid1
. Similarly,A(:,id2)
has all rows of the columns specified by vectorid2
.Given an array
A
and subscript vectorsid1
,id2
, …,idn
, the resulting arrayA(id1,id2,…,idn)
has the sizenumel(id1)
-by-numel(id2)
-by-…-by-numel(idn)
.
When you specify subscript vectors id1
, id2
, id3
, and so on, to access elements of an array A
, the number of subscripts must not exceed the number of dimensions returned by ndims(A)
. The exception is that you can include any number of trailing subscripts equal to 1, because arrays in MATLAB can have any number of trailing dimensions of size 1. For more information, see Arrays with Dimensions of Size 1.
The following examples demonstrate these rules.
Create a 3-by-3 matrix A
. Access the fourth, fifth, and sixth elements of this matrix by specifying the subscript row vector id = 4:6
as the linear indices. The result is a 1-by-3 row vector, which is the same size as the subscript row vector id
.
A = magic(3)
A = 3×3
8 1 6
3 5 7
4 9 2
id = 4:6
id = 1×3
4 5 6
v = A(id)
v = 1×3
1 5 9
Create a 1-by-6 column vector A
. Access the fourth, fifth, and sixth elements of this vector by specifying the subscript row vector id = 4:6
as the indices. The result is a 3-by-1 column vector, which has the same number of elements as id
but has the orientation of the column vector A
.
A = (1:6)'
A = 6×1
1
2
3
4
5
6
id = 4:6
id = 1×3
4 5 6
v = A(id)
v = 3×1
4
5
6
Next, create a 4-by-4 matrix A
. Access the elements of A
that are located in the second to fourth rows and the second to fourth columns by specifying two subscript vectors id1
and id2
. The result is a 3-by-3 matrix, which has the dimensions numel(id1)
-by-numel(id2)
.
A = magic(4)
A = 4×4
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
id1 = 2:4; id2 = 2:4; B = A(id1,id2)
B = 3×3
11 10 8
7 6 12
14 15 1
If you want to access the elements of A
that are located at (2, 2), (3, 3), and (4, 4) instead, you can use the sub2ind
function to obtain the linear indices of these locations.
idLin = sub2ind(size(A),id1,id2)
idLin = 1×3
6 11 16
B = A(idLin)
B = 1×3
11 6 1
Note that the syntax A(id1,id2)
follows the second rule, while the syntax A(idLin)
follows the first rule, as discussed previously.
For the third rule, create a 2-by-3-by-2 array A
. Access the elements of A
that are located in the first row, the second and third columns, and the first and second pages by specifying three subscript vectors id1
, id2
, and id3
. The result is a 1-by-2-by-2 array, which has the size of numel(id1)
-by-numel(id2)
-by-numel(id3)
.
A = randi([1 5],2,3,2)
A = A(:,:,1) = 5 1 4 5 5 1 A(:,:,2) = 2 5 1 3 5 5
id1 = 1; id2 = 2:3; id3 = 1:2; B = A(id1,id2,id3)
B = B(:,:,1) = 1 4 B(:,:,2) = 5 1
size(B)
ans = 1×3
1 2 2
Convert Subscript Matrices to Vectors
The previous examples demonstrated indexing with subscript vectors. While MATLAB supports subscript matrices and multidimensional arrays as indices, you can convert these indices to subscript vectors to access specific array elements, as discussed in the following four examples.
Sort Each Row of Matrix
Create two 3-by-3 matrices that contain numbers and their corresponding labels.
A = magic(3)
A = 3×3
8 1 6
3 5 7
4 9 2
B = ["h" "a" "f"; "c" "e" "g"; "d" "i" "b"]
B = 3×3 string
"h" "a" "f"
"c" "e" "g"
"d" "i" "b"
Sort the elements of each row of the matrix A
. Specify two output arguments when using the sort
function to return the sorted matrix Asorted
as well as the indices id
as a matrix. The id
matrix is a collection of 1-by-3 row index vectors that describe the rearrangement of each row of A
.
[Asorted,id] = sort(A,2)
Asorted = 3×3
1 6 8
3 5 7
2 4 9
id = 3×3
2 3 1
1 2 3
3 1 2
Use the returned indices to sort the array B
. To do this, you can use each row of id
as a subscript vector to access the elements of B
. Put these steps in a for
-loop and show the result.
Bsorted = B; for i = 1:size(B,1) Bsorted(i,:) = B(i,id(i,:)); end Bsorted
Bsorted = 3×3 string
"a" "f" "h"
"c" "e" "g"
"b" "d" "i"
Specify Row and Column Locations
Create a 4-by-4 matrix.
A = magic(4)
A = 4×4
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
Specify the row and column locations to access the elements of A
as a matrix. This matrix specifies the elements of A
located at (1, 2), (2, 3), and (3,4).
M = [1 2; 2 3; 3 4]
M = 3×2
1 2
2 3
3 4
Convert these element locations to linear indices by using the sub2ind
function.
id = sub2ind(size(A),M(:,1),M(:,2))
id = 3×1
5
10
15
Use these linear indices, which form a subscript vector, to access the elements of A
.
A(id)
ans = 3×1
2
10
12
Specify Linear Indices
Create a 3-by-3 matrix.
A = magic(3)
A = 3×3
8 1 6
3 5 7
4 9 2
Suppose that you have another matrix that contains the linear indices to access the elements of A
. Convert these linear indices to a subscript column vector.
M = [2 5; 3 4; 4 3]; id = M(:)
id = 6×1
2
3
4
5
4
3
Access the elements of A
by using this subscript column vector. The result is a 6-by-1 column vector that is the same size as the subscript vector.
B = A(id)
B = 6×1
3
4
1
5
1
4
To rearrange the accessed elements to have the same size as M
, use the reshape
function.
B = reshape(B,size(M))
B = 3×2
3 5
4 1
1 4
Access Submatrix
Create a 5-by-5 matrix.
A = magic(5)
A = 5×5
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
Suppose that you have two matrices that contain the row and column locations of the submatrix of A
that you want to access. Convert these two matrices to two subscript column vectors. The first subscript vector indicates that the row locations you want to access are 1, 2, 4, and 3, and the second subscript vector indicates that the column locations you want to access are 1, 2, 5, and 3.
Mrow = [1 4; 2 3]; Mcol = [1 5; 2 3]; id1 = Mrow(:)
id1 = 4×1
1
2
4
3
id2 = Mcol(:)
id2 = 4×1
1
2
5
3
Access the submatrix of A
by using these two subscript vectors.
B = A(id1,id2)
B = 4×4
17 24 15 1
23 5 16 7
10 12 3 19
4 6 22 13
If you want to access the elements of A
that are located at (1, 1), (2, 2), (4, 5), and (3,3) instead of the rectangular submatrix of A
, you can use the steps described in Specify Row and Column Locations. Convert the two vectors id1
and id2
to linear indices by using the sub2ind
function.
idLin = sub2ind(size(A),id1,id2)
idLin = 4×1
1
7
24
13
Use these linear indices, which form a subscript vector, to access the elements of A
.
B = A(idLin)
B = 4×1
17
5
3
13
Indexing with Fewer Subscripts Than Array Dimensions
When you access a multidimensional array with fewer subscripts than the dimensions of the array, MATLAB treats the array as if it has a number of dimensions equal to the number of subscripts you specified. MATLAB effectively folds or reshapes all subsequent dimensions, starting from the location of the last subscript, into a single dimension. For example, if you have a 2-by-3-by-2 array and use two subscripts to access its elements, MATLAB treats the array as a 2-by-6 array.
Create a 3-D array with dimensions 2-by-3-by-2.
A = rand(2,3,2)
A = A(:,:,1) = 0.9572 0.8003 0.4218 0.4854 0.1419 0.9157 A(:,:,2) = 0.7922 0.6557 0.8491 0.9595 0.0357 0.9340
Access the element in the second row, second column, and second page of A
by specifying three subscripts—2, 2, and 2—as the indices.
v = A(2,2,2)
v = 0.0357
If you specify only one subscript when accessing the array A
, then the subscript is a linear index for all appended columns of A
. In this case, MATLAB treats A
as a 12-by-1 column vector. For example, access the 10th element of A
by specifying a linear index 10
.
v = A(10)
v = 0.0357
If you specify two subscripts using the colon operators when accessing A
, then the first colon represents all elements in the first dimension of A
, and the second colon represents all elements in the folded second and third dimensions of A
. In this case, MATLAB folds the first and second pages of A
. The second colon operator can be considered as the linear index for all elements in the folded second and third dimensions of A
.
Afolded = A(:,:)
Afolded = 2×6
0.9572 0.8003 0.4218 0.7922 0.6557 0.8491
0.4854 0.1419 0.9157 0.9595 0.0357 0.9340
Equivalently, you can reshape A
to a 2-by-6 array, where the last two dimensions with size 3-by-2 are folded into a single dimension with six elements.
Afolded = reshape(A,2,6)
Afolded = 2×6
0.9572 0.8003 0.4218 0.7922 0.6557 0.8491
0.4854 0.1419 0.9157 0.9595 0.0357 0.9340
Consequently, you can also access a specific element of A
, which has the size of 2-by-3-by-2, by specifying only two subscripts. For example, you can access the element A(2,5)
, which is the element in the second row and the fifth folded column of the last two dimensions of A
.
v = A(2,5)
v = 0.0357
A(2,5)
, A(2,2,2)
, and A(10)
all return the same element of A
.