mode - Most frequent values in array

Syntax

M = mode(X)
M = mode(X, dim)
[M,F] = mode(X, ...)
[M,F,C] = mode(X, ...)

Description

M = mode(X) for vector X computes the sample mode M, (i.e., the most frequently occurring value in X). If X is a matrix, then M is a row vector containing the mode of each column of that matrix. If X is an N-dimensional array, then M is the mode of the elements along the first nonsingleton dimension of that array.

When there are multiple values occurring equally frequently, mode returns the smallest of those values. For complex inputs, this is taken to be the first value in a sorted list of values.

M = mode(X, dim) computes the mode along the dimension dim of X.

[M,F] = mode(X, ...) also returns array F, each element of which represents the number of occurrences of the corresponding element of M. The M and F output arrays are of equal size.

[M,F,C] = mode(X, ...) also returns cell array C, each element of which is a sorted vector of all values that have the same frequency as the corresponding element of M. All three output arrays M, F, and C are of equal size.

Remarks

The mode function is most useful with discrete or coarsely rounded data. The mode for a continuous probability distribution is defined as the peak of its density function. Applying the mode function to a sample from that distribution is unlikely to provide a good estimate of the peak; it would be better to compute a histogram or density estimate and calculate the peak of that estimate. Also, the mode function is not suitable for finding peaks in distributions having multiple modes.

Examples

Example 1

Find the mode of the 3-by-4 matrix shown here:

X = [3 3 1 4; 0 0 1 1; 0 1 2 4]
X =
     3     3     1     4
     0     0     1     1
     0     1     2     4

mode(X)
ans =
     0     0     1     4

Find the mode along the second (row) dimension:

mode(X, 2)
ans =
     3
     0
     0

Example 2

Find the mode of a continuous variable grouped into bins:

randn('state', 0);       % Reset the random number generator

y = randn(1000,1);
edges = -6:.25:6;
[n,bin] = histc(y,edges);

m = mode(bin)
m =
    22

edges([m, m+1])
ans =
   -0.7500   -0.5000

hist(y,edges+.125)

See Also

mean, median, hist, histc

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS