Acquiring related Matrix Members

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 日

0 投票

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 日

0 投票

out = A(sub2ind(size(A),(1:numel(B))',B(:)));

カテゴリ

ヘルプ センター および File ExchangeOperators and Elementary Operations についてさらに検索

質問済み:

2012 年 8 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by