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 CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by