Matrix evaluation at a collection of indices

Hello,
I would like to do multiple matrix evaluations without the use of a loop. In particular, suppose Z is a p-by-q matrix, and X and Y are m-by-n integer-valued matrices.
I would like to construct an m-by-n matrix U defined as
U(i,j) = Z(X(i,j),Y(i,j))
Is there a simple way to do this without using the loop? Thanks a lot!

 Accepted Answer

idx = sub2ind([m n],i1,j1);
U = zeros([m n]);
U(idx) = Z(X(idx),Y(idx));

More Answers (0)

Categories

Find more on Sparse Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!