How to get the indices of max values in a matrix,and map them to the indices of another same sized matrix?

StackOverflow https://stackoverflow.com/questions/22961424

Question

I have two 16x12 matrices, In matrix A, I should sort in descending order and find the first 10 max values. But I should know the max values' indices before being sorted.

Finally, I should give those indices to the second matrix and find the values in that matrix.

I tried with for-loop but it doesn't give me accurate answer.

Était-ce utile?

La solution

This should work:

[~,I] = sort(A(:), 'descend');
vals = B(I(1:10));

For example:

>> A = [ 4 2; 1 5];
>> B = [ 7 8; 0 NaN];
>> [~,I] = sort( A(:), 'descend' );
>> vals = B(I(1:2))

vals =
  NaN
  7    
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top