Let’s review indexing techniques in Matlab: Indexing one dimensional array, two dimensional array, logical indexing, reversiong a vector – are covered.
Consider a sample vector in Matlab.
>> x=[9 21 6 8 7 3 18 -1 0 4]
ans=
9 21 6 8 7 3 18 -1 0 4
Index with single value
>> x(2)
ans =
21
Index with range of values
>> x([1 4 6])
ans =
9 8 3
Select a range of elements using ‘:’ operator.
Example: Select elements with index ranging from 1 to 5.
>> x(1:5)
ans =
9 21 6 8 7
Making a new vector by swapping two halves of the vector x
>> x([6:10 1:5])
ans =
3 18 -1 0 4 9 21 6 8 7
To refer the last element of the matrix x use ‘end’ operator
>> x(end)
ans =
4
You can do arithmetic on the ‘end’ operator. Selecting all elements except the last element
>> x(1:end-1)
ans =
9 21 6 8 7 3 18 -1 0
Reversing the vector
>> x(end:-1:1)
ans =
4 0 -1 18 3 7 8 6 21 9
Using the end operator in a range. Selecting from fifth element to the end
>> x(5:end)
ans =
7 3 18 -1 0 4
Replacing specific elements in the array by placing the new values on the right side of the expression. Replacing the values of ‘x’ at positions [2,5,8] with new values
>> x([2 5 8])=[11 14 -6]
x =
9 11 6 8 14 3 18 -6 0 4
Replacing specific elements with a same value. Replacing the values of ‘x’ at position [1 and 10] with 40
>> x([1 10]) = 40
x =
40 11 6 8 14 3 18 -6 0 40
Two dimensional Arrays:
>> x=magic(4) %Create a magic array with 4x4 elements
x =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
The two dimensional array elements are accessed with two subscript indices like x(i,j) – where ‘i’ represents row and ‘j’ represents column. Accessing the element located at second row and third column
>> x(2,3)
ans =
10
The subscript indices can also be vectors. Selecting the elements in the first row – 4th,2nd and 1st column & third row – 4th,2nd and 1st column
>> x([1 3],[4 2 1])
ans =
13 2 16
12 7 9
The ‘:’ operator is the short form for 1:end. It is usually used to select all the elements in a specific column of row. Accessing all elements on the third row
>> x(3,:)
ans =
9 7 6 12
Extracting the last row using the “end” operator and the “:” operator
>> x(end,:)
ans =
4 14 15 1
Logical Indexing:
Used to select the elements of a matrix that satisfy some criteria given by an expression
>> x=magic(4) %Create a magic array with 4x4 elements
x =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
Getting all elements of the matrix whose value is above 10
>> x(x>10)
ans =
16
11
14
15
13
12
Getting the row and column subscripts of those elements using the “find” function.
>> [i,j]=find(x>10)
i =
1
2
4
4
1
3
j =
1
2
2
3
4
4
Sometimes, when you run your script you might encounter ‘Inf’ (IEEE arithmetic representation for positive infinity) or -Inf( representation for negative infinity) values sitting in a matrix. See the following example
>> k=[1 4 5 0 6 7 0 -1]
x=1./k %Finding the reciprocal of each element and storing it as a vector
k =
1 4 5 0 6 7 0 -1
x =
1.0000 0.2500 0.2000 Inf 0.1667 0.1429 Inf -1.0000
Your rest of the script may need you to fix this by replacing the Inf with 0. This can be achieved by using “isinf” function in Matlab. The “isinf” function returns a logical array which will specify whether an element is Inf (It also compares -Inf).
>> x(isinf(x))=0
x =
1.0000 0.2500 0.2000 0 0.1667 0.1429 0 -1.0000
There are other functions similar to “isinf” like “isspace”,”isnan” etc.., that look for the specific condition to satisfy and returns a logical array depending on the result.
Browse articles tagged for Matlab code…
Rate this article: Note: There is a rating embedded within this post, please visit this post to rate it.
Other Resources on Matlab’s Matrix Indexing:
[1] Matlab documentation on Matrix indexing↗
[2]A Book detailing how MATLAB code can be written in order to maximize its effectiveness.
Richard K Johnson, “The Elements of MATLAB Style “, Cambridge University Press,ISBN-13: 978-0521732581↗
Thanks! I had a similar document of my own but not half as comprehensive as this. I often forget the indexing as I shift from different languages.