MATLAB: How to find maximum and second maximum number from vector.

random

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

Best Answer

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)