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

1 回表示 (過去 30 日間)
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!

採用された回答

Matt J
Matt J 2017 年 10 月 31 日
編集済み: Matt J 2017 年 10 月 31 日
[m,n]=size(A);
L=sub2ind([m,n], (1:m).' , max_index);
result = B(L)

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by