Indices from Max function to Extract Data
2 views (last 30 days)
Show older comments
Rajesh Rajaram
on 11 Mar 2015
Commented: Rajesh Rajaram
on 11 Mar 2015
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 Comments
Andrew Newell
on 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?
Accepted Answer
Andrei Bobrov
on 11 Mar 2015
Edited: Andrei Bobrov
on 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 Comments
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!