MATLAB: How to find the third largest number in a matrix?

maxminsort

Hey,
Please help me find the third largest element in a matrix. I also want to find its position so I don't think sort() will work.
I found this max(A(A<max(A(:)))) to work but it only show the second number not the third.
Best,

Best Answer

First sort it in descending order
[sortedMatrix, sortOrder] = sort(yourMatrix(:), 'descend');
Then take the third element of each vector:
thirdLargest = sortedMatrix(3)
originalIndex = sortOrder(3) % A linear index.
% Convert from linear subscript to row and column.
[row, column] = ind2sub(size(yourMatrix), originalIndex)
Why do you say that won't work?