Azzera filtri
Azzera filtri

Indices from Max function to Extract Data

3 visualizzazioni (ultimi 30 giorni)
I have two arrays:
A=[1 2; 3 2; 3 4];
B=[1 2; 3 4; 5 6];
When I calculate [M,I] = max(A,[],2), I get
M =
2
3
4
I =
2
1
2
Which is fine. Now I want to report values of B for the When A=M in each row; that is:
1
4
5
Is there a way to avoid using the for loop to build my answer array (have a large data set)? I used:
[I_row, I_col] = ind2sub(size(A),I)
but that doesn't seem to help my cause. Kindly help
Thanks in advance
  2 Commenti
Andrew Newell
Andrew Newell il 11 Mar 2015
Your question gets a little garbled in the middle, and I'm not sure what you're after. If you want the values [1 4 5] from B, those correspond to the position of the minima in each row of A. Is that what you want?
Rajesh Rajaram
Rajesh Rajaram il 11 Mar 2015
So sorry for the confusion. My bad!! I just need the values of B corresponding to the position which has the max values of A.
2 3 6 (and not what I typed. SORRY!!)
I have since found the expression B(sub2ind(size(A), 1:size(A,1), I'))' which seems to do the trick.
But if I want the dim in max function to be changed from 2 to 1, then I have to manually input for sub2ind to get my desired answer.
Is there a generic expression to get the right answer irrespective of the dim that I use in the max function?

Accedi per commentare.

Risposta accettata

Andrei Bobrov
Andrei Bobrov il 11 Mar 2015
Modificato: Andrei Bobrov il 11 Mar 2015
A=[1 2; 3 2; 3 4];
B=[1 2; 3 4; 5 6];
n = 1;
[~,I]=max(A,[],n);
c ={(1:size(A,3-n))',I(:)}; % EDIT
l = n == 1;
out = B(sub2ind(size(A),c{[l,~l]+[1,1]}));
  3 Commenti

Accedi per commentare.

Più risposte (0)

Tag

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by