Acquiring related Matrix Members

2 vues (au cours des 30 derniers jours)
Umit
Umit le 7 Août 2012
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?

Réponse acceptée

Matt Fig
Matt Fig le 7 Août 2012
Modifié(e) : Matt Fig le 7 Août 2012
A(sub2ind(size(A),1:length(B),B(:)'))
or
A((1:length(B))+(B(:)'-1)*size(A,1))
  2 commentaires
Andrei Bobrov
Andrei Bobrov le 7 Août 2012
Hi Matt!
Matt Fig
Matt Fig le 7 Août 2012
Hi Andrei :-).

Connectez-vous pour commenter.

Plus de réponses (1)

Andrei Bobrov
Andrei Bobrov le 7 Août 2012
Modifié(e) : Andrei Bobrov le 7 Août 2012
out = A(sub2ind(size(A),(1:numel(B))',B(:)));

Catégories

En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by