I want to find the third maximum value in matrix. I already have the max value
max(A)
And I already have second max value
max(A(A~=max(A))
But i cannot do the third one, please advice and help me.
I want to find the third maximum value in matrix. I already have the max value
max(A)
And I already have second max value
max(A(A~=max(A))
But i cannot do the third one, please advice and help me.
The simplest solution would be to sort the values of A
in descending order, and pick the third sorted element (if it exists):
A_sorted = sort(A(:), 'descend');
third_max = A_sorted(min(3, end));
If you don't allow repeating values (e.g A = [10, 10; 9; 2]
and want 2), sort the unique values:
A_sorted = sort(unique(A), 'descend');