MATLAB: Hello, I didn’t get the following code:

component-wise indexing with arraysMATLAB

M = reshape(linspace(11,18,8),[2,2,2])
M([1,2],[2,1],[2,1])
A1 = [2,2;2,1]; v = [2,1];
M(A1,v,1)
*****************************
ans =
14 12
14 12
14 12
13 11
i didnt get this ans, more precisely, im confused that what A1=[2,2;2,1] in M(A1,v,1) do???
************************************************************************

Best Answer

This is all simple indexing which I thought was well explained in the doc
M([1,2],[2,1],[2,1])
returns row 1 and 2, column 2 and 1 (in that order) and page 2 and 1 (in that order) of M.
A1 = [2,2;2,1];
v = [2,1];
M(A1,v,1)
Note that the shape of A1 is irrelevant in the above. A1 = [2 2 2 1] would produce the same result. It returns row 2, then row 2 again, then row 2 again, then row 1 and column 2 and 1 (in that order) and page 1.
v(1) = index column 2
| v(2) = index column 1
| |
V V
14 12 <-- A1(1) = index row 2
14 12 <-- A1(2) = index row 2
14 12 <-- A1(3) = index row 2
13 11 <-- A1(4) = index row 1