Categories &

Functions List

Function Reference: median

: m = median (x)
: m = median (x, dim)
: m = median (x, vecdim)
: m = median (x, "all")
: m = median (…, nanflag)
: m = median (…, outtype)

Compute the median value of the elements of x.

When the elements of x are sorted, say s = sort (x), the median is defined as $$ {\rm median} (x) = \cases{s(\lceil N/2\rceil), & $N$ odd;\cr (s(N/2)+s(N/2+1))/2, & $N$ even.} $$

If x is an array, then median (x) operates along the first non-singleton dimension of x.

The optional variable dim forces median to operate over the specified dimension, which must be a positive integer-valued number. Specifying any singleton dimension in x, including any dimension exceeding ndims (x), will result in a median equal to x.

Specifying the dimensions as vecdim, a vector of non-repeating dimensions, will return the median over the array slice defined by vecdim. If vecdim indexes all dimensions of x, then it is equivalent to the option "all". Any dimension in vecdim greater than ndims (x) is ignored.

Specifying the dimension as "all" will force median to operate on all elements of x, and is equivalent to median (x(:)).

median (…, outtype) returns the median with a specified data type, using any of the input arguments in the previous syntaxes. outtype can take the following values:

"default"

Output is of type double, unless the input is single in which case the output is of type single.

"double"

Output is of type double.

"native".

Output is of the same type as the input (class (x)), unless the input is logical in which case the output is of type double.

The optional variable nanflag specifies whether to include or exclude NaN values from the calculation using any of the previously specified input argument combinations. The default value for nanflag is "includenan" which keeps NaN values in the calculation. To exclude NaN values set the value of nanflag to "omitnan". The output will still contain NaN values if x consists of all NaN values in the operating dimension.

See also: mean, mode, movmedian

Source Code: median