No License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

Join the 15-year community celebration.

Play games and win prizes!

» Learn more

4.8 | 5 ratings Rate this file 9 Downloads (last 30 days) File Size: 567 Bytes File ID: #5266 Version: 1.0
image thumbnail




16 Jun 2004 (Updated )

Statistical mode.

| Watch this File

File Information

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.


This file inspired Fastmode.

MATLAB release MATLAB 6.5 (R13)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (9)
13 Apr 2006 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));

Comment only
27 Mar 2006 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);

19 Mar 2006 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.

Comment only
13 Mar 2006 anonymous a.

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

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

Comment only
29 Nov 2004 Asaf Tsoar


17 Nov 2004 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?

Comment only
15 Nov 2004 Uzma Saeed

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

10 Nov 2004 Brian Murphy

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

29 Aug 2004 Darrel Francis

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

many thanks!

Contact us