Quantcast

Documentation Center

  • Trial Software
  • Product Updates

mode

Most frequent values in array

Syntax

Description

example

M = mode(X) returns the sample mode of X, which is the most frequently occurring value in X. When there are multiple values occurring equally frequently, mode returns the smallest of those values. For complex inputs, the smallest value is the first value in a sorted list.

  • If X is a vector, then mode(X) returns the most frequent value of X.

  • If X is a nonempty matrix, then mode(X) returns a row vector containing the mode of each column of X.

  • If X is an empty 0-by-0 matrix, mode(X) returns NaN.

  • If X is a multidimensional array, then mode(X) acts along the first nonsingleton dimension and returns an array of most frequent values. The size of this dimension reduces to 1 while the sizes of all other dimensions remain the same.

example

M = mode(X,dim) returns the mode of elements along dimension dim. For example, if X is a matrix, then mode(X,2) is a column vector containing the most frequent value of each row

example

[M,F] = mode(___) also returns a frequency array F, using any of the input arguments in the previous syntaxes. F is the same size as M, and each element of F represents the number of occurrences of the corresponding element of M.

example

[M,F,C] = mode(___) also returns a cell array C of the same size as M and F. Each element of C is a sorted vector of all values that have the same frequency as the corresponding element of M.

Examples

expand all

Mode of Matrix Columns

Define a 3-by-4 matrix.

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

Find the most frequent value of each column.

M = mode(X)
M =

     0     0     1     4

Mode of Matrix Rows

Define a 3-by-4 matrix.

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

Find the most frequent value of each row.

M = mode(X,2)
M =

     3
     0
     0

Mode of 3-D Array

Create a 1-by-3-by-4 array of integers between 1 and 10.

X = gallery('integerdata',10,[1,3,4],1)
X(:,:,1) =

    10     8    10


X(:,:,2) =

     6     9     5


X(:,:,3) =

     9     6     1


X(:,:,4) =

     4     9     5

Find the most frequent values of this 3-D array along the second dimension.

M = mode(X)
M(:,:,1) =

    10


M(:,:,2) =

     5


M(:,:,3) =

     1


M(:,:,4) =

     4

This operation produces a 1-by-1-by-4 array by finding the most frequent value along the second dimension. The size of the second dimension reduces to 1.

Compute the mode along the first dimension of X.

M = mode(X,1);
isequal(X,M)
ans =

     1

This returns the same array as X because the size of the first dimension is 1.

Mode of Matrix Columns with Frequency Information

Define a 3-by-4 matrix.

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

Find the most frequent value of each column, as well as how often it occurs.

[M,F] = mode(X)
M =

     0     0     1     4


F =

     2     1     2     2

F(1) is 2 since M(1) occurs twice in the first column.

Mode of Matrix Rows with Frequency and Multiplicity Information

Define a 3-by-4 matrix.

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

Find the most frequent value of each row, how often it occurs, and which values in that row occur with the same frequency.

[M,F,C] = mode(X,2)
M =

     3
     0
     0


F =

     2
     2
     1


C = 

    [         3]
    [2x1 double]
    [4x1 double]

C{2} is the 2-by-1 vector [0;1] since values 0 and 1 in the second row occur with frequency F(2).

C{3} is the 4-by-1 vector [0;1;2;4] since all values in the third row occur with frequency F(3).

Mode of 16-bit Unsigned Integer Array

Define a 1-by-4 vector of 16-bit unsigned integers.

X = gallery('integerdata',10,[1,4],3,'uint16')
X =

      6      3      2      3

Find the most frequent value, as well as the number of times it occurs.

[M,F] = mode(X),
class(M)
M =

      3


F =

     2


ans =

uint16

M is the same class as the input, X.

Input Arguments

expand all

X — Input arrayvector | matrix | multidimensional array

Input array, specified as a vector, matrix, or multidimensional array.

NaN values in the input array, X, are ignored.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

dim — Dimension to operate alongpositive integer scalar

Dimension to operate along, specified as a positive integer scalar. If no value is specified, the default is the first array dimension whose size does not equal 1.

Dimension dim indicates the dimension whose length reduces to 1. The size(M,dim) is 1, while the sizes of all other dimensions remain the same.

Consider a two-dimensional input array, X.

  • If dim = 1, then mode(X,1) returns a row vector containing the most frequent value in each column.

  • If dim = 2, then mode(X,2) returns a column vector containing the most frequent value in each row.

mode returns X if dim is greater than ndims(X).

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Output Arguments

expand all

M — Most frequent valuesscalar | vector | matrix | multidimensional array

Most frequent values returned as a scalar, vector, matrix, or multidimensional 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.

The class of M is the same as the class of the input array, X.

F — Frequency arrayscalar | vector | matrix | multidimensional array

Frequency array returned as a scalar, vector, matrix, or multidimensional array. The size of F is the same as the size of M, and each element of F represents the number of occurrences of the corresponding element of M.

The class of F is always double.

C — Most frequent values with multiplicitycell array

Most frequent values with multiplicity returned as a cell array. The size of C is the same as the size of M and F, and each element of C is a sorted column vector of all values that have the same frequency as the corresponding element of M.

More About

expand all

First Nonsingleton Dimension

The first nonsingleton dimension is the first dimension of an array whose size is not equal to 1.

For example:

  • If X is a 1-by-n row vector, then the second dimension is the first nonsingleton dimension of X.

  • If X is a 1-by-0-by-n empty array, then the second dimension is the first nonsingleton dimension of X.

  • If X is a 1-by-1-by-3 array, then the third dimension is the first nonsingleton dimension of X.

Tips

  • 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.

See Also

| | | |

Was this topic helpful?