Function to find the more recurrent number
조회 수: 1 (최근 30일)
이전 댓글 표시
Hye, I have a matrix 1 by (a big number, like 1000), and I would like to extract the element of the matrix that appears most of the time.
Can you help me?
댓글 수: 0
채택된 답변
Friedrich
2011년 8월 8일
Hi,
i think the hist function can help here:
%create some random numbers between 1 and 20
a = ceil(rand(100,1)*20);
%put them in as much bin as different numbers exists
n = hist(a,numel(unique(a)));
%show the histogram
hist(a,numel(unique(a)));
%number appears most
element = find(n == max(n))
%numel(find(a==element)) should return the same as max(n)
댓글 수: 2
Friedrich
2011년 8월 8일
You have to do:
>> Y_un = unique(Y);
>> n = hist(Y,numel(Y_un));
>> element = find(n == max(n));
>> most_occ = Y_un(element)
추가 답변 (3개)
Paulo Silva
2011년 8월 8일
Another possible way
a=randi([1 20],1,1000);
u=unique(a);
[C,I]=max(arrayfun(@(x)sum(a==u(x)),1:numel(u)));
disp('The value that appears most times is:')
u(I)
disp('Number of times it appears:')
C
In case of having two values that appear the same number of times it will choose just one of them.
댓글 수: 0
huda nawaf
2011년 8월 10일
hi, see it please,
X=[1 1 1 2 3 4 4 4 0]; >> v=mode(X) ??? No appropriate methods for function mode. . I need the mode which compute frequencies of appearing of number. thanks
참고 항목
카테고리
Help Center 및 File Exchange에서 Adaptive Control에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!