How do I find the Nth high number in a matrix A ?

Hi guys,
I need to find the 5th high number of a matrix. Using the max() function I can only have the 1st one.
Thanks for helping!

 Akzeptierte Antwort

Stephen23
Stephen23 am 25 Sep. 2014
Bearbeitet: Stephen23 am 25 Sep. 2014

0 Stimmen

As Iain suggested, sort is a great way to go, but as you mention that you have a matrix, you might want to sort every element of the matrix:
sorted = sort(values(:));
sorted(end-4)

Weitere Antworten (2)

Iain
Iain am 25 Sep. 2014

0 Stimmen

values = 1:500;
sorted = sort(values,'descend');
sorted(5)
José-Luis
José-Luis am 25 Sep. 2014

0 Stimmen

If you want to take care of repeated values:
vals = randi(200,10);
uVals = sort( unique(vals(:)) );
result = uVals(end-4);

Kategorien

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by