MATLAB: Indexing one matrix using elements of another

indexingmatrix

I have a matrix p=[1,2,4] and want to retrieve elements of another matrix u =[1,11,7;2,3,5;9,10,8;15,16,17], using the column of p combined with its entry e.g. I want a matrix z=[1,3,17]
z(1,1) = u(entry in p(col1), 1); z(1,2) = u(entry in p(col2),2); z(1,3)=u(entry in p(col3), 3).

Best Answer

You'll need to use linear indexing (and convert your subscripts to get them):
z = u(sub2ind(size(u),p,1:numel(p)))