I have an array with numbers that range from 1 to 4. I need to know which is/are the values that repeat more. In case there's a draw I need to know which are the values so I can make some operations.
Example:
a = [1 1 1 2 2 2 3 4]
Output = [1 2]
a = [1 1 1 2 3 4]
Output = 1
a = [1 2 2 3 3 4 4]
Output = [2 3 4]
Any ideas?
Alternate vectorised approach using hist
and unique
uVal = unique(a);
counts = hist(a,uVal);
out = uVal(counts == max(counts));
Results:
a = [1 1 1 2 2 2 3 4];
>> out
out =
1 2
The third output of mode
gives just that. The input vector doesn't need to be sorted.
[~, ~, v] = mode(a);
result = v{1};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With