File Exchange

image thumbnail


version (449 Bytes) by Michael Robbins
Statistical mode.

1 Download

Updated 05 Apr 2006

No License

MODE finds the mode of a sample. The mode is the observation with the greatest frequency.

i.e. in a sample x=[0, 1, 0, 0, 0, 3, 0, 1, 3, 1, 2, 2, 0, 1] the mode is the most frequent item, 0.

Cite As

Michael Robbins (2021). mode (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (10)

joe joe

Harold Bien

Timing a single loop is not accurate. To time performance, you need to do several iterations with a large dataset. Using a (slightly) modified version of your code, the time it takes to determine the mode of a 10,000 random element: >> data=rand(10000,1); y=mode(data);
yields the following timing results (where I surrounded both methods with a for i=1:1000 loop:

Hist method: 9.49, 9.61, 9.53secs
Non-hist method: 3.23, 3.32, and 3.20secs

I swapped the order (both loops ran in same function) and the timing remained the same.

Command line I used and results:
Elapsed time is 9.489020 seconds.
Elapsed time is 3.234487 seconds.
>> m=mode(data);
Elapsed time is 9.606668 seconds.
Elapsed time is 3.318920 seconds.
>> m=mode(data); % Order swapped (non-hist first, hist second)
Elapsed time is 3.197718 seconds.
Elapsed time is 9.529675 seconds.

for i=1:1000
[d1, i1]=unique(sorted);
[d2, i2]=max(h);

for i=1:1000
[b,i,j] = unique(x);
h = hist(j,length(b));

Kuncup Iswandy

The mode function seems to return only one solution when there are more than one solution, e.g. X = [1 1 0 2 3 3]; this function only returns 1. It should 1 and 3.

Here I give a little bit modifications

function y = modestat(x)

[b,i,j] = unique(x);
[m,k] = sort(hist(j,length(b)));
id = find(max(m) == m);
y = k(id);

Michael Robbins

Thanks guys, but I tested it and your way was 50% slower.

Elapsed time is 0.010098 seconds.
Elapsed time is 0.015903 seconds.

anonymous a.

FYI, here's a corrected version of Harold's suggestion above:

[d1, i1]=unique(sorted);
[d2, i2]=max([i1(1); diff(i1)]);

Asaf Tsoar


Harold Bien

What about instead of using 'hist' use the distance between the indices returned from unique (only works for sorted data), a la:

[dummy, idx]=unique(sorted);
[dummy, idx]=max(diff(idx));

Is this faster?

Uzma Saeed

handy function...seved me some time, good work and smart solution

Brian Murphy

This is just what I needed for a quick function, you've saved me some time. Thanks!

Darrel Francis

great stuff! was trying to do this myself using hist but never thought of preselecting the bins using unique.

many thanks!

MATLAB Release Compatibility
Created with R13
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired: fastmode

Community Treasure Hunt

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

Start Hunting!