In MATLAB, there are three main methods to access array elements according to the position (index) of elements in the array: position index, linear index and logical index.
Index by element position
The most common method is to explicitly specify the index of the element. For example, to access an element in a matrix, specify the row and column numbers of the element in order.
A = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]A = 4×4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 e = A(3,2)e = 10
e is the element at position 3 and 2 (the third row and the second column) in A.
You can also specify the indexes of multiple elements in a vector to reference multiple elements at once. For example, access the first and third elements in the second row of A.
r = A(2,[1 3])r = 1×2 5 7
To access elements within a row range or column range, use colon. For example, access the elements in the first to third rows and the second to fourth columns of A.
r = A(1:3,2:4)r = 3×3 2 3 4 6 7 8 10 11 12
Another way to calculate r is to use the keyword end to specify the second to last column. With this method, you can directly specify the last column without knowing how many columns are in A.
r = A(1:3,2:end)r = 3×3 2 3 4 6 7 8 10 11 12
If you want to access all rows or all columns, just use the colon operator. For example, return the entire third column of A.
r = A(:,3)r = 4×1 3 7 11 15
In general, indexes can be used to access the elements of any array in MATLAB, regardless of its data type or dimension. For example, directly access the columns of the datetime array.
t = [datetime(2018,1:5,1); datetime(2019,1:5,1)]t = 2x5 datetime 01-Jan-2018 01-Feb-2018 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 datetime 01-Mar-2018 01-Mar-2019
For higher dimensional arrays, you can extend the syntax to match the array dimensions. Suppose there is a random 3 × three × 3 numerical array. Access the elements in the second row and third column of the first page of the array.
A = rand(3,3,3);e = A(2,3,1)e = 0.5469
Index using a single index
Another way to access array elements is to use only a single index, regardless of the size or dimension of the array. This method is called linear indexing. Although MATLAB displays the array according to the defined size and shape, in fact, the array is stored as a single column element in memory. We can use matrices to intuitively understand this concept. The following array is displayed as 3 × 3 matrix, but MATLAB stores it as a single column, which is successively connected by the columns of A. The stored vector contains a sequence of elements 12, 45, 33, 36, 29, 25, 91, 48 and 11, which can be all displayed with a single colon.
A = [12 36 91; 45 29 48; 33 25 11]A = 3×3 12 36 91 45 29 48 33 25 11 Alinear = A(:)Alinear = 9×1 12 45 33 36 29 25 91 48 11
For example, the 3rd and 2nd element of A is 25, and you can use the syntax A(3,2) Access it. You can also use syntax A(6) Access this element because 25 is the sixth element in the stored vector sequence.
e = A(3,2)e = 25 elinear = A(6)elinear = 25
Linear indexes may not be visually intuitive, but they are useful when performing some calculations that do not depend on the size or shape of the array. For example, you can easily sum all elements of A without specifying the second parameter of the sum function.
s = sum(A(:))s = 330
The sub2ind and ind2sub functions can be used to convert between the original index and the linear index of an array. For example, calculate the linear index of the 3rd and 2nd elements of A.
linearidx = sub2ind(size(A),3,2)linearidx = 6
Convert from linear index back to row and column form.
[row,col] = ind2sub(size(A),6)row = 3 col = 2
Index using logical values
Arrays can also be indexed using the true and false logical indicators, which is particularly convenient when processing conditional statements. For example, suppose you want to know whether the element in matrix A is smaller than the corresponding element in another matrix B. When the element in a is less than the corresponding element in B, the less than operator returns a logical array with an element of 1.
A = [1 2 6; 4 3 6]A = 2×3 1 2 6 4 3 6 B = [0 3 7; 3 7 5]B = 2×3 0 3 7 3 7 5 ind = A<Bind = 2x3 logical array 0 1 1 0 1 0
Now that you know the location of the elements that meet the conditions, you can use ind as the index array to check the values. MATLAB matches the position of the value 1 in ind with the corresponding elements in A and B, and lists their values in the column vector.
Avals = A(ind)Avals = 3×1 2 3 6 Bvals = B(ind)Bvals = 3×1 3 7 7
The is function in MATLAB also returns a logical array indicating which elements in the input meet specific conditions. For example, use the ismissing function to check which elements in a string vector are missing values.
str = ["A" "B" missing "D" "E" missing];ind = ismissing(str)ind = 1x6 logical array 0 0 1 0 0 1
Suppose you want to find the value of a non missing value element. take ~ Operator and index vector ind can be used together to achieve this purpose.
strvals = str(~ind)strvals = 1x4 string "A" "B" "D" "E"