Acquiring related Matrix Members

2 次查看(过去 30 天)
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))

更多回答(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 中查找有关 Multidimensional Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by