필터 지우기
필터 지우기

how to find maximum and second maximum number from vector.

조회 수: 3 (최근 30일)
Talat
Talat 2011년 3월 30일
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

채택된 답변

Jos (10584)
Jos (10584) 2011년 3월 30일
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)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by