Filter löschen
Filter löschen

how to find maximum and second maximum number from vector.

1 Ansicht (letzte 30 Tage)
Talat
Talat am 30 Mär. 2011
i need to find the maximum and second maximum occurrence of number from vector. . e.g A=[1 2 3 2 2 2 3 3 3 3 3 2 2 45 5 5 5 4 2 2 2 2 2] when i write 'mode(A)'. it only returns one maximum occurrence of number which is '2' in this case. now i have to find the second maximum numbers from same vector. please write its code how to find it? thanx

Akzeptierte Antwort

Jos (10584)
Jos (10584) am 30 Mär. 2011
One of the easier alternatives:
m1 = mode(x)
xcopy = x ; xcopy(x==m1) = []
m2 = mode(xcopy)
Another options uses unique, histc and sort:
ux = unique(x) ;
n = histc(x,ux) ;
[ns, si] = sort(n,[], 'descend') ;
ux = ux(si) % sort in same order
ux(1:2)

Weitere Antworten (0)

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by