Indexing allows referencing separate matrix elements or submatrices. We will use the following matrices throughout all the examples:
a = 10 * ((1:5) + (0:5:20)')
b = 10 * (1:6)
c = 10 * (1:6)'
a = 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190 200 210 220 230 240 250 b = 10 20 30 40 50 60 c = 10 20 30 40 50 60
a(2, 3) # element of the 2nd row, the 3rd column
a(5, 5) # element of the 5th row, the 5th column
a(2, 3) = 81 # you can either read element, or assign to them
b(3) # b and c are one-dimentional matrices, we may use only one index
c(4)
c(4, 1) # but we still can use both indices: 4th row, 1st column
ans = 81 ans = 250 a = 10 20 30 40 50 60 70 81 90 100 110 120 130 140 150 160 170 180 190 200 210 220 230 240 250 ans = 30 ans = 40 ans = 40
Althogh, $a$ is a two dimentional matrix, you can anyway use one index to access its elements. The elements are counted from the leftmost topmost element down column by column:
a(3)
a(8)
a(8) = 122
ans = 110 ans = 120 a = 10 20 30 40 50 60 70 81 90 100 110 122 130 140 150 160 170 180 190 200 210 220 230 240 250
Indexes are not restricted to be only numbers. They may be matrices by themselves. In this case we select several lines and columns and get a submatrix.
row = 3;
col = 4;
a(row, col) # a usual way to index elements
rows = [3, 4];
cols = [3, 4, 5];
a(rows, cols) # selecting a submatrix
a([3, 4, 3], [1, 2]) # it is possible to select some row or column several times
ans = 140 ans = 130 140 150 180 190 200 ans = 110 120 160 170 110 120
A very common usecase is to select a whole row or a whole column:
a(1, [1, 2, 3, 4, 5]) # we select the whole 1st row, because we mention all the columns
a(1, 1:5) # the same may be achived by the range operator. 1:5 just equals [1 2 3 4 5]
a(1, 1:end) # there is a magical word `end`, that always means "the last element"
a(1, 1:end-1) # we may do evaluations with `end`, e.g. take all but the last element
a(1, (end+1)/2:end) # we may take the second half of the row
a(1, :) # there is a special syntax for the `1:end` range. Just write :
ans = 10 20 30 40 50 ans = 10 20 30 40 50 ans = 10 20 30 40 50 ans = 10 20 30 40 ans = 30 40 50 ans = 10 20 30 40 50
This is actually a very common code, to select rows and columns with the : symbol:
a(2, :) # the second row
a(:, 3) # the third column
a(:, :) # select everything (not very usefull, because this is the same as `a`)
ans = 60 70 81 90 100 ans = 30 81 130 180 230 ans = 10 20 30 40 50 60 70 81 90 100 110 120 130 140 150 160 170 180 190 200 210 220 230 240 250
Indexing submatrices may be used to assignments:
a([1, 2], [3, 4]) # a 2 by 2 submatrix
a([1, 2], [3, 4]) = [31 41; 82 91] # we may assign a matrix of the same size
a(4:5, 4:5) # again we have a 2 by 2 submatrix
a(4:5, 4:5) = 200 # and it is possible to assign one number, in this case it is copied to all the cells
aa = a # we will do the next operation on the copy of the matrix:
aa(:, :) = 1 # copy 1 to all elements of the matrix
ans = 31 41 82 91 a = 10 20 31 41 50 60 70 82 91 100 110 120 130 140 150 160 170 180 200 200 210 220 230 200 200 ans = 200 200 200 200 a = 10 20 31 41 50 60 70 82 91 100 110 120 130 140 150 160 170 180 200 200 210 220 230 200 200 aa = 10 20 31 41 50 60 70 82 91 100 110 120 130 140 150 160 170 180 200 200 210 220 230 200 200 aa = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
The next example is a litle bit complicated, but remember what is the effect of indexing a two-dimentional matrix with only one index:
a(:)
ans = 10 60 110 160 210 20 70 120 170 220 31 82 130 180 230 41 91 140 200 200 50 100 150 200 200