How to index a matrix with matrixes?
A short outline of the matrix indexing will be followed by
a=[1,2,3,4;5,6,7,8;9,10,11,12]
b=[1,2;3,3;2,3]
c=[1;2]
octave:6> a(b,c)
ans =
1 2
9 10
5 6
5 6
9 10
9 10
Indexing Vectors (https://de.mathworks.com/company/newsletters/articles/matrix-indexing-in-matlab.html)
Let's start with the simple case of a vector and a single subscript. The vector is:
v = [16 5 9 4 2 11 7 14];
The subscript can be a single value:
v(3) % Extract the third element
ans =
9
Or the subscript can itself be another vector:
v([1 5 6]) % Extract the first, fifth, and sixth elements
ans =
16 2 11
The crucial point here is item 1 selects item 1 of v which is 16 and
item 5 selects item 5 of v which is 2 and
item 6 selects item 6 of v which is 11.
------------------------------------------------
Let's take an a matrix with octave:
C:\Users\ars>octave -qf
octave:1> a=[1,2,3,4;5,6,7,8;9,10,11,12]
a =
1 2 3 4
5 6 7 8
9 10 11 12
Let's take a b matrix:
octave:2> b=[1,2;3,4;5,6]
b =
1 2
3 4
5 6
Let's take a c matrix:
octave:3> c=[1;2]
c =
1
2
octave:4> a(b,c)
error: A(I,J): row index out of bounds; value 6 out of bound 3
Octave does not like 6 because a has only 3 rows.
Let's take b with only one item bigger than a's row count.
octave:4> b=[1,2;3,4;2,3]
b =
1 2
3 4
2 3
octave:5> a(b,c)
error: A(I,J): row index out of bounds; value 4 out of bound 3
The error repeats. Hence, the items of b matrix must not be
bigger than the row count of a matrix.
Let's take a b matrix with all of its items less or equal to
the row count of a matrix.
octave:5> b=[1,2;3,3;2,3]
b =
1 2
3 3
2 3
Let's try a(b,c) again:
octave:6> a(b,c)
ans =
1 2
9 10
5 6
5 6
9 10
9 10
It works. Let's recall a and c matrixes and analyze this result:
a =
1 2 3 4
5 6 7 8
9 10 11 12
c =
1
2
We have to refer for each of b the related row
and each of c to the realed column of a matrix.
b=1 c=1,2 -> 1 2
b=3 c=1,2 -> 9 10
b=2 c=1,2 -> 5 6
Please notice that b points to each row according to the value of item in sequence
of b 1 5 9 2 6 10 3 7 11 4 8 12
and for each row c proceeds for each column of each row with the value of items in matrix sequence
of c that is 1 2 1 2 1 2 1 2 1 2 ...
namely
b=2 c=1,2 -> 5 6
b=3 c=1,2 -> 9 10
b=3 c=1,2 -> 9 10
Just another example:
octave:7> b=[1,1;2,2;3,3]
b =
1 1
2 2
3 3
Let's recal a and c matrixes:
a =
1 2 3 4
5 6 7 8
9 10 11 12
c =
1
2
octave:8> a(b,c)
ans =
1 2
5 6
9 10
1 2
5 6
9 10
c selects first and second columns. b selects
1 2 3 1 2 3 rows.
Let's increase the rows count of b. And observe:
octave:9> b=[1,1;2,2;3,3;1,1]
b =
1 1
2 2
3 3
1 1
octave:10> a(b,c)
ans =
1 2
5 6
9 10
1 2
1 2
5 6
9 10
1 2
Increasing the size of b (or c) does not cause an
abnormal end. It only increases the selected items
of the output matrix. Infact
the size of the output matrix = size(b) times size(c).
Below given examples show that, the size and shape of the
b and c matrixes a(b,c) does not cause an abend because
the a(b,c) calculation is done according to the item
sequence of b and c matrixes.
octave:11> b=[1,1,1;2,2,2;3,3,3]
b =
1 1 1
2 2 2
3 3 3
octave:12> a(b,c)
ans =
1 2
5 6
9 10
1 2
5 6
9 10
1 2
5 6
9 10
octave:13> c=[1,2,2]
c =
1 2 2
octave:14> a(b,c)
ans =
1 2 2
5 6 6
9 10 10
1 2 2
5 6 6
9 10 10
1 2 2
5 6 6
9 10 10
octave:15> c=[1,2;3,4]
c =
1 2
3 4
octave:16> a(b,c)
ans =
1 3 2 4
5 7 6 8
9 11 10 12
1 3 2 4
5 7 6 8
9 11 10 12
1 3 2 4
5 7 6 8
9 11 10 12
Cheers.
Ali Riza SARAL
arsaral((at))yahoo.com