MATLAB: N=A(B) where A and B are matrices

matrix manipulation

Hello, I just tried to do the following operation,
>> B=full(B)
B =
5 1 8 3 2 8
9 9 8 8 3 6
1 1 1 4 2 5
4 7 4 9 2 2
>> A=full(A)
A =
1 8 2 6 5 9
4 3 6 7 1 2
2 5 3 7 3 8
>> A(B)
ans =
3 1 6 2 4 6
3 3 6 6 2 5
1 1 1 8 4 3
8 2 8 3 4 4
I would like to know what exactly has happened here. Thank you.

Best Answer

A(B) returns the value of the B(ith) element of A. The elements are counted column-wise, what means than in your case: A(1) = 1; A(2) = 4; A(3) = 2; A(4) = 8; A(5) = 3; ... A(18) = 8;
Now, if you take the matrix B as index, you get back a matrix whose elements are the elements of A in the position determined by the value of the element of B:
B(1) = 5
then, the first element of A(B) = A(5) = 3.
B(2) = 9
then, the second element of A(B) = A(9) = 3
B(3) = 1
then, the second element of A(B) = A(1) = 1
and so on.