user3269865
user3269865

Reputation: 49

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

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.

Upvotes: 0

Views: 67

Answers (1)

Lisa
Lisa

Reputation: 3526

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    

Upvotes: 2

Related Questions