# How to find the mode of an array?

6 views (last 30 days)
Garrett Oliekan on 23 Jan 2022
Commented: Garrett Oliekan on 24 Jan 2022
I am trying to write code for a program to find the mode of an array. This will obvioulsy not get me the answer yet, but I am working on a few theories and this is one start of one of the. This tool is part of a homework assignment but the actual assignment is different. I have created a dummy array of numbers to practice with. The eventual assignment will use a lot of user inputs and some more complicated math.
The mode should be "4" as it is the only number that is repeated in the array.
My goal is to use loops to check if a number is equal to any number in the array besides itself. If it is, I would add it to the blank array "y". The actual situation might contain multiple modes, which is why I want to sort out all the repeat values.
I cannot use any predefined matlab functions or commands.
The output I am getting is weird, I assume it is because of how I have defined the parameters of the loops. Can someone help with my logic here?
x=[2,3,4,1,5,4];
y=[];
for a=1:6
for b=1:5
if x(a)==x(b+1) && x(a)~=x(b) %This is supposed to mean if x(a) is equal to another number in the array but not equal to itself...
% I also tried if x(a)==x(b) && x(a)~=x(b-1) but that gave me an error as x(b-1) does not exist when b=1.
y(end+1)=x(a);
end
end
end
display(y)

_ on 23 Jan 2022
Edited: _ on 23 Jan 2022
One thing you can do is set your inner loop ('b' loop) to start at the index one after the current value of a. This way you never compare an element to itself and you don't compare each pair of elements more than once. So the order would be: compare element 1 to element 2, then 1 to 3, then 1 to 4, 1 to 5, 1 to 6, then the 'b' loop ends and the 'a' loop goes to the next iteration and you compare element 2 to element 3, 2 to 4, 2 to 5, 2 to 6, then next 'a' iteration: 3 to 4, 3 to 5, 3 to 6, and so on.
I've made this change to the first method you mentioned (actually the second method is equivalent to the first, if I understand what you are intending to do there, so I'm just using the first method here). Let's see what it gives:
x=[2,3,4,1,5,4];
y=[];
for a=1:6
for b=a+1:6
if x(a)==x(b)
y(end+1)=x(a);
end
end
end
display(y)
y = 4
OK, so we got the mode (4). But what happens if I add another 2 and another 4 to the end of x? In this case the mode is still 4, but now 2 is repeated as well. Let's see what the method does:
x=[2,3,4,1,5,4,2,4];
y=[];
for a=1:8
for b=a+1:8
if x(a)==x(b)
y(end+1)=x(a);
end
end
end
display(y)
y = 1×4
2 4 4 4
So it got that 2 and 4 exist more than once in x, and the 2 seems ok, but what's with the three 4's? That's because there are three pairs of 4s in x: (element 3, element 6), elements (3, 8), and elements (6, 8). Each of these pairs is counted separately. Include another 4 to see:
x=[2,3,4,1,5,4,2,4,4];
y=[];
for a=1:9
for b=a+1:9
if x(a)==x(b)
y(end+1)=x(a);
end
end
end
display(y)
y = 1×7
2 4 4 4 4 4 4
That's six 4s for six pairs of 4s in x. There are four 4s in x, and taking each pair of them gives nchoosek(4,2) = 4!/(2!*(4-2)!) = 6 pairs.
OK, so this method gives extra 4's but still, 4 is the mode, so that's not necessarily a problem. We just have to be able to know that there are more 4's than 2's in the output vector y. And actually, that's the same as finding the mode of y, so this problem is equivalent to the one we started out trying to solve. Basically we've done something to x to get y in an attempt to find the mode of x, and now we just need to find the mode of y, so we're back where we started, right? If we had a method to get the mode of y, we could just apply that method to x and get the mode of x. There would be no need to calculate y at all. So maybe rethinking our approach would be worthwhile at this point.
OK. The mode is the value or values that appear the most, so how about counting the number of times each value occurs in x? (Then the mode would be the value(s) with the maximum count.) To do the counting, we can loop over elements of x, but the key is we'd need to keep track of which values have been counted so far and how many times they've been counted. I won't write down code for this, since you asked for guidance with your logic, but here's an outline of how it might work.
x=[2,3,4,1,5,4,2,4,4];
counted = []; % values that have been counted at least once so far
counts = []; % the number of times each counted value has appeared so far
for i = 1:numel(x) % (I'm going to assume numel() is OK to use)
% - figure out if x(i) has been counted so far by comparing x(i) to each
% element of the vector 'counted'
% - if there is a match (i.e., x(i) has previously appeared), then
% increment its count by 1 and move on to the next element of x
% - if there is no match for x(i) in 'counted' (i.e., x(i) has not
% previously appeared), append x(i) to 'counted' and set its count to
% 1 (i.e., append 1 to 'counts')
end
At the end of that process, for this vector x, you would get:
counted = [2 3 4 1 5];
counts = [2 1 4 1 1];
So that you'd know 2 appears 2 times, 3 appears 1 time, 4 appears 4 times, etc.
Then it's just a matter of grabbing the element(s) of counted where counts == max(counts) without using the max() function, and this is/those are your mode(s).
Garrett Oliekan on 24 Jan 2022
Thanks Benjamin! This really helps!

Garrett Oliekan on 23 Jan 2022
I have also tried a different approach by using different variables..
x=[2,3,4,1,5,4];
repeatcount = 0; %starts a count for if a variable is repeating.
for a=1:6
for b=1:5
if x(a)==x(b)
repeatcount = repeatcount + 1; %counts how many times a number in the array is the same as another number in the
array (including itself)
end
end
if repeatcount>1
repeats=zeros(x(a),1); %if the number is equal to another number in the array (besides itself),
% x(a) gets added to an array called "repeates"
repeatcount=0; %resets repeatcount after every loop of a...
end
end
display(x)
display(repeats)

R2021b

### Community Treasure Hunt

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

Start Hunting!