0

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.

Bow House
  • 149
  • 3
  • 10
  • 1
    related question: [Find n minimum values in an array](http://stackoverflow.com/questions/14774860/) – Eitan T Jul 15 '13 at 16:54

1 Answers1

4

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');
Eitan T
  • 32,660
  • 14
  • 72
  • 109