Need to find value in one matrix that corresponds to the max value of each row in another matrix

1 view (last 30 days)
I have a matrix let's say, A. I have found the max value of each row and the index to which it corresponds. Now I need to find the values in another matrix, B, that correspond to the max indices of A.
A = [20 1 3;
7 3 4;
6 9 5]
[max_value max_index] = max(A,[],2)
So that
max_index = [1; 1; 2]
B = [4 6 11;
10 20 15;
1 5 9]
However, every way I can think to do this gives me the following output:
B(max_index) = [4; 4; 10]
Because max_index only includes the column number from A where max_value is instead of the row and column associated with the max_value. Is there a way to get
B(max_index) = [4; 10; 5]
I have tried transposing max_index, this does nothing to help. I have tried using a for loop, but may not have used to correct implementation here. Any help would be greatly appreciated! Thanks!

Accepted Answer

Matt J
Matt J on 31 Oct 2017
Edited: Matt J on 31 Oct 2017
[m,n]=size(A);
L=sub2ind([m,n], (1:m).' , max_index);
result = B(L)

More Answers (0)

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!