Main Content

median

Median value of array

Description

example

M = median(A)returns the median value ofA.

  • IfAis a vector, thenmedian(A)returns the median value ofA.

  • IfAis a nonempty matrix, thenmedian(A)treats the columns ofAas vectors and returns a row vector of median values.

  • IfAis an empty 0-by-0 matrix,median(A)returnsNaN.

  • IfAis a multidimensional array, thenmedian(A)treats the values along the first array dimension whose size does not equal1as vectors. The size of this dimension becomes1while the sizes of all other dimensions remain the same.

mediancomputes natively in the numeric class ofA, such thatclass(M) = class(A).

example

M = median(A,'all')computes the median over all elements ofA. This syntax is valid for MATLAB®versions R2018b and later.

example

M = median(A,dim)returns the median of elements along dimensiondim. For example, ifAis a matrix, thenmedian(A,2)is a column vector containing the median value of each row.

example

M = median(A,vecdim)computes the median based on the dimensions specified in the vectorvecdim. For example, ifAis a matrix, thenmedian(A,[1 2])is the median over all elements inA, since every element of a matrix is contained in the array slice defined by dimensions 1 and 2.

example

M = median(___,nanflag)optionally specifies whether to include or omitNaNvalues in the median calculation for any of the previous syntaxes. For example,median(A,'omitnan')ignores allNaNvalues inA.

Examples

collapse all

Define a 4-by-3 matrix.

A = [0 1 1; 2 3 2; 1 3 2; 4 2 2]
A =4×30 1 1 2 3 2 1 3 2 4 2 2

Find the median value of each column.

M = median(A)
M =1×31.5000 2.5000 2.0000

For each column, the median value is the mean of the middle two numbers in sorted order.

Define a 2-by-3 matrix.

A = [0 1 1; 2 3 2]
A =2×30 1 1 2 3 2

Find the median value of each row.

M = median(A,2)
M =2×11 2

For each row, the median value is the middle number in sorted order.

Create a 1-by-3-by-4 array of integers between1and10.

rng('default') A = randi(10,[1,3,4])
A = A(:,:,1) = 9 10 2 A(:,:,2) = 10 7 1 A(:,:,3) = 3 6 10 A(:,:,4) = 10 2 10

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

M = median(A)
M = M(:,:,1) = 9 M(:,:,2) = 7 M(:,:,3) = 6 M(:,:,4) = 10

This operation produces a 1-by-1-by-4 array by computing the median of the three values along the second dimension. The size of the second dimension is reduced to1.

Compute the median along the first dimension ofA.

M = median(A,1); isequal(A,M)
ans =logical1

This command returns the same array asAbecause the size of the first dimension is1.

Create a 3-D array and compute the median over each page of data (rows and columns).

A(:,:,1) = [2 4; -2 1]; A(:,:,2) = [6 2; -5 3]; A(:,:,3) = [4 4; 7 -3]; M1 = median(A,[1 2])
M1 = M1(:,:,1) = 1.5000 M1(:,:,2) = 2.5000 M1(:,:,3) = 4

Starting in R2018b, to compute the median over all dimensions of an array, you can either specify each dimension in the vector dimension argument, or use the'all'option.

M2 = median(A,[1 2 3])
M2 = 2.5000
Mall = median(A,'all')
Mall = 2.5000

Define a 1-by-4 vector of 8-bit integers.

A = int8(1:4)
A =1x4 int8 row vector1 2 3 4

Compute the median value.

M = median(A),
M =int83
class(M)
ans = 'int8'

Mis the mean of the middle two numbers in sorted order returned as an 8-bit integer.

Create a vector and compute its median, excludingNaNvalues.

A = [1.77 -0.005 3.98 -2.95 NaN 0.34 NaN 0.19]; M = median(A,'omitnan')
M = 0.2650

Input Arguments

collapse all

Input array, specified as a vector, matrix, or multidimensional array.Acan be a numeric array, ordinalcategoricalarray,datetimearray, ordurationarray.

Dimension to operate along, specified as a positive integer scalar. If you do not specify the dimension, then the default is the first array dimension of size greater than 1.

Dimensiondimindicates the dimension whose length reduces to1. Thesize(M,dim)is1,而所有其他维度的大小保持the same.

Consider anm-by-ninput matrix,A:

  • median(A,1)computes the median of the elements in each column ofAand returns a1-by-nrow vector.

    median(A,1) column-wise operation

  • median(A,2)computes the median of the elements in each row ofAand returns anm-by-1column vector.

    median(A,2) row-wise operation

medianreturnsAwhendimis greater thanndims(A).

Vector of dimensions, specified as a vector of positive integers. Each element represents a dimension of the input array. The lengths of the output in the specified operating dimensions are 1, while the others remain the same.

Consider a 2-by-3-by-3 input array,A. Thenmedian(A,[1 2])returns a 1-by-1-by-3 array whose elements are the medians of each page ofA.

Mapping of a 2-by-3-by-3 input array to a 1-by-1-by-3 output array

NaNcondition, specified as one of these values:

  • 'includenan'——输入包含的值ingNaNvalues is alsoNaN.

  • 'omitnan'— allNaNvalues appearing in the input are ignored. Note: theNaNflags are not set to0.

You also can specify additional values for some data types.

  • 'includeundefined'and'omitundefined'categoricalinput

  • 'includenat'and'omitnat'datetimeinput

Algorithms

For ordinal categorical arrays, MATLAB interprets the median of an even number of elements as follows:

If the number of categories between the middle two values is ... Then the median is ...
zero (values are from consecutive categories) larger of the two middle values
an odd number value from category occurring midway between the two middle values
an even number value from larger of the two categories occurring midway between the two middle values

Extended Capabilities

Version History

Introduced before R2006a