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?
No products are associated with this question.
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)
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.