MATLAB: Sub2ind Get values of 3D matrix using an index vector

3d matrixindex vector

Dear all,
I have a 37*30*100000 matrix (call matrix A), and a 37*1 vector which contains elements are indexes of the column for each row I want to get the value.
In other word, I want to get a 37*1*1000 matrix (matrix B) from matrix A, using index vector v to get the value corresponding to the column of index specifying by vector v.
Is there anyone having an idea how to work with this, not using for loop (the dimension is very huge!). I think sub2ind might work, but dont know how to adapt it for my situation with 3D matrix.
Thanks,

Best Answer

[m,n,p] = size(A);
AR = reshape(A,[m*n, p]);
B = AR(sub2ind([m,n],(1:m)',C(:)),:);
B = reshape(B,[m 1 p])