Indexing one matrix using elements of another

3 次查看(过去 30 天)
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).
  1 个评论
Garima Sharma
Garima Sharma 2017-7-18
And I'm hoping for a solution without loops since my actual p matrix is 1000*1000, so I'd want the same behavior to replicate across rows of p. E.g. if p(2,1)=1, then z(2,1)=1, if p(2,2)=4, then z(2,2)=16.

请先登录,再进行评论。

采纳的回答

the cyclist
the cyclist 2017-7-18
编辑:the cyclist 2017-7-18
You'll need to use linear indexing (and convert your subscripts to get them):
z = u(sub2ind(size(u),p,1:numel(p)))

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by