Effacer les filtres
Effacer les filtres

how to find maximum and second maximum number from vector.

1 vue (au cours des 30 derniers jours)
Talat
Talat le 30 Mar 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

Réponse acceptée

Jos (10584)
Jos (10584) le 30 Mar 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)

Plus de réponses (0)

Catégories

En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by