Acquiring related Matrix Members

조회 수: 2 (최근 30일)
Umit
Umit 2012년 8월 7일
I have a n by m matrix A and 1 by n array B. Assume
A = rand(3,5)
B = [3;5;2]
How can I get 1st rows 3rd element, 2nd rows 5th element and 3rd rows 2. element of A matrix w/o for loop?
Each rows related element is the member of B array as you can see?

채택된 답변

Matt Fig
Matt Fig 2012년 8월 7일
편집: Matt Fig 2012년 8월 7일
A(sub2ind(size(A),1:length(B),B(:)'))
or
A((1:length(B))+(B(:)'-1)*size(A,1))
  댓글 수: 2
Andrei Bobrov
Andrei Bobrov 2012년 8월 7일
Hi Matt!
Matt Fig
Matt Fig 2012년 8월 7일
Hi Andrei :-).

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2012년 8월 7일
편집: Andrei Bobrov 2012년 8월 7일
out = A(sub2ind(size(A),(1:numel(B))',B(:)));

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by