Main Content

Array Indexing

In MATLAB®, there are three primary approaches to accessing array elements based on their location (index) in the array. These approaches are indexing by position, linear indexing, and logical indexing.

Indexing with Element Positions

The most common way is to explicitly specify the indices of the elements. For example, to access a single element of a matrix, specify the row number followed by the column number of the element.

A = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
A =4×41 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
e = A(3,2)
e = 10

eis the element in the 3,2 position (third row, second column) ofA.

You can also reference multiple elements at a time by specifying their indices in a vector. For example, access the first and third elements of the second row ofA.

r = A(2,[1 3])
r =1×25 7

To access elements in a range of rows or columns, use thecolon. For example, access the elements in the first through third row and the second through fourth column ofA.

r = A(1:3,2:4)
r =3×32 3 4 6 7 8 10 11 12

An alternative way to computeris to use the keywordendto specify the second column through the last column. This approach lets you specify the last column without knowing exactly how many columns are inA.

r = A(1:3,2:end)
r =3×32 3 4 6 7 8 10 11 12

If you want to access all of the rows or columns, use the colon operator by itself. For example, return the entire third column ofA.

r = A(:,3)
r =4×13 7 11 15

In general, you can use indexing to access elements of any array in MATLAB regardless of its data type or dimensions. For example, directly access a column of adatetimearray.

t = [datetime(2018,1:5,1); datetime(2019,1:5,1)]
t =2x5 datetime01 - 2018年1月- 2018年01 - 2月01-Mar-2018 01-Apr-2018 01-May-2018 01-Jan-2019 01-Feb-2019 01-Mar-2019 01-Apr-2019 01-May-2019
march1 = t(:,3)
march1 =2x1 datetime01-Mar-2018 01-Mar-2019

For higher-dimensional arrays, expand the syntax to match the array dimensions. Consider a random 3-by-3-by-3 numeric array. Access the element in the second row, third column, and first sheet of the array.

A = rand(3,3,3); e = A(2,3,1)
e = 0.5469

For more information on working with multidimensional arrays, seeMultidimensional Arrays.

Indexing with a Single Index

Another method for accessing elements of an array is to use only a single index, regardless of the size or dimensions of the array. This method is known aslinear indexing. While MATLAB displays arrays according to their defined sizes and shapes, they are actually stored in memory as a single column of elements. A good way to visualize this concept is with a matrix. While the following array is displayed as a 3-by-3 matrix, MATLAB stores it as a single column made up of the columns ofAappended one after the other. The stored vector contains the sequence of elements12,45,33,36,29,25,91,48,11, and can be displayed using a single colon.

A = [12 36 91; 45 29 48; 33 25 11]
A =3×312 36 91 45 29 48 33 25 11
Alinear = A(:)
Alinear =9×112 45 33 36 29 25 91 48 11

For example, the 3,2 element ofAis25, and you can access it using the syntaxA(3,2). You can also access this element using the syntaxA(6), since25is sixth element of the stored vector sequence.

e = A(3,2)
e = 25
elinear = A(6)
elinear = 25

While linear indexing can be less intuitive visually, it can be powerful for performing certain computations that are not dependent on the size or shape of the array. For example, you can easily sum all of the elements ofAwithout having to provide a second argument to thesumfunction.

s = sum(A(:))
s = 330

Thesub2indandind2subfunctions help to convert between original array indices and their linear version. For example, compute the linear index of the 3,2 element ofA.

linearidx = sub2ind(size(A),3,2)
linearidx = 6

Convert from the linear index back to its row and column form.

[row,col] = ind2sub(size(A),6)
row = 3
col = 2

Indexing with Logical Values

Using true and false logical indicators is another useful way to index into arrays, particularly when working with conditional statements. For example, say you want to know if the elements of a matrixAare less than the corresponding elements of another matrixB. The less-than operator returns a logical array whose elements are1when an element inAis smaller than the corresponding element inB.

A = [1 2 6; 4 3 6]
A =2×31 2 6 4 3 6
B = [0 3 7; 3 7 5]
B =2×30 3 7 3 7 5
ind = A
               
ind =2x3 logical array0 1 1 0 1 0

Now that you know the locations of the elements meeting the condition, you can inspect the individual values usingindas the index array. MATLAB matches the locations of the value 1 inindto the corresponding elements ofAandB, and lists their values in a column vector.

Avals = A(ind)
Avals =3×12 3 6
Bvals = B(ind)
Bvals =3×13 7 7

MATLAB "is" functions also return logical arrays that indicate which elements of the input meet a certain condition. For example, check which elements of astringvector are missing using theismissingfunction.

str = ["A""B"missing"D""E"missing]; ind = ismissing(str)
ind =1x6 logical array0 0 1 0 0 1

Suppose you want to find the values of the elements that arenotmissing. Use the~operator with the index vectorindto do this.

strvals = str(~ind)
strvals =1x4 string"A" "B" "D" "E"

For more examples using logical indexing, seeFind Array Elements That Meet a Condition.

Related Topics

External Websites