I have matrix
A=magic(4)*10
and matrix
B=[6 6 2 6; 1 1 2 1; 7 7 2 1; 3 3 -1 3 ]
I want to get values of A depending on values in B. For example:
A=
160 20 30 130
50 110 100 80
90 70 60 120
40 140 150 10
B=
6 6 2 6
6 6 2 2
2 6 2 6
6 2 -1 2
I want to start at A(1,1) (value=160). Then, If the corresponding value on B, B(1,1) = 6, I want to get the element that is on the right, A(1,2), and if B(1,1) = 2, I want to get value that is below, A(2,1). Finally, if the value is -1, I do not want to proceed. In the end I want to get result like this:
result=
160
20
30
100
60
Can anyone help me please?
Try the following code: