Indexing one matrix using elements of another

조회 수: 1 (최근 30일)
Garima Sharma
Garima Sharma 2017년 7월 18일
댓글: Garima Sharma 2017년 7월 18일
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에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by