matlab - How to find the third maximum value in matrix? -


this question has answer here:

i want find third maximum value in matrix. have max value

max(a) 

and have second max value

max(a(a~=max(a)) 

but cannot third one, please advice , me.

the simplest solution sort values of a in descending order, , pick third sorted element (if exists):

a_sorted = sort(a(:), 'descend'); third_max = a_sorted(min(3, end)); 

if don't allow repeating values (e.g a = [10, 10; 9; 2] , want 2), sort unique values:

a_sorted = sort(unique(a), 'descend'); 

Comments