MATLAB: I have 2D array of size 16*16 stored in a variable temp and I have set of x and y values x=[1 2 3]; y=[4 5 6]; I want to extract values from temp variable indexed by (x,y)=(1,4) (2,5) (3,6) without using for loop. Thank you.

matrix manipulation

I have 2D array of size 16*16 stored in a variable temp and I have set of x and y values x=[1 2 3]; y=[4 5 6]; I want to extract values from temp variable indexed by (x,y)=(1,4) (2,5) (3,6) without using for loop. Thank you.

Best Answer

values = YourMatrix( sub2ind(size(YourMatrix), x, y) );
The faster form of this for a 16 x 16 matrix is
YourMatrix(x + (y-1)*16)