Function to find the more recurrent number

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?

 Accepted Answer

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 Comments

Let say we have this vector: Y= 0.0084 0.0084 0.0084 0.0084 0.0084 0.0084 0.0084 0.0084 0.0084 0.0084 5.9139 5.9432 5.9140 5.9432 5.9434 5.9139
The element that appear the most is 0.0084, and that is the answer I want to extract in this case. I want the program to do it by itself. I used the line x=find(Y==max(Y)), but it give me 15. What have I done wrong?
You have to do:
>> Y_un = unique(Y);
>> n = hist(Y,numel(Y_un));
>> element = find(n == max(n));
>> most_occ = Y_un(element)

Sign in to comment.

More Answers (3)

Much more simple:
x = mode(Y);

5 Comments

I tried it, but I received the message:
??? Undefined command/function 'mode'.
You must have an earlier MATLAB. mode() has been part of MATLAB for several versions, but I would have to research to find out when it was introduced.
the mode function was introduced in the Service Pack 3 of Release 14 back in Setember 2005, I think it's the first time I see it, thanks for bring it up Walter :)
function X=modi(Y)
%Y is a vector, it cannot be a matrix, reshape it before entering it in
%modi.
%First, FOR MY NEED, I adjust the value of Y
Y=round(10000*Y)/10000;
%Parameter used in the boucle.
n=length(Y);
%Preallocation of the memories for the matrix T
T(2,n)=0;
%For the first iteration, the first row of T is the different value of Y,
%the second is number of time the value appears.
T(1,1)=Y(1);
T(2,1)=1;
%This is a parameter to know where will go the next new value in the matrix
%T.
s=1;
for j=2:n %First boucle to pass every element of Y.
for i=1:s %Second boucle to compare the value of Y(j) with every value of T.
k=1; %Parameter of control to know if the value exist in T or not
if Y(j)==T(1,i) %Condition to adjust the count of the value Y(j), if not already in T, the value will be added after.
T(2,i)=T(2,i)+1;
k=0;
break
end
end
if k==1 %If the value didn't exist in T, it is now added.
s=s+1;
T(1,s)=Y(j);
T(2,s)=1;
end
end
%Now looking for the most recurrent value by comparing the value of the
%second row.
H=T(2,1);
d=1;
for j=2:s
if H<T(2,j)
H=T(2,j);
d=j;
end
end
%Time to give the answer.
X=T(1,d);
I've programmed one that dos the job for me, if someone need it. Just make sure it's good for you, cause at the line 5, I added a line that might create some problem, you can just erase it.

Sign in to comment.

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.
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

1 Comment

function T=modi(Y)
%Y is a vector, it cannot be a matrix, reshape it before entering it in
%modi.
%Parameter used in the boucle.
n=length(Y);
%Preallocation of the memories for the matrix T
T(2,n)=0;
%For the first iteration, the first row of T is the different value of Y,
%the second is number of time the value appears.
T(1,1)=Y(1);
T(2,1)=1;
%This is a parameter to know where will go the next new value in the matrix
%T.
s=1;
for j=2:n %First boucle to pass every element of Y.
for i=1:s %Second boucle to compare the value of Y(j) with every value of T.
k=1; %Parameter of control to know if the value exist in T or not
if Y(j)==T(1,i) %Condition to adjust the count of the value Y(j), if not already in T, the value will be added after.
T(2,i)=T(2,i)+1;
k=0;
break
end
end
if k==1 %If the value didn't exist in T, it is now added.
s=s+1;
T(1,s)=Y(j);
T(2,s)=1;
end
end
for j=n:-1:1
if T(2,j)==0
T(:,j)=[];
else
break
end
end
I change my code, and this one should return you a matrix of two row. The first row gives you the different number, and the second row return you the frequencies of their appearence. I didn't test it.

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!