Reputation: 49
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
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