Function Reference: median

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

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 a matrix, then median (x) returns a row vector with the mean of each column in x.
  • If x is a multidimensional array, then median (x) operates along the first non-singleton dimension of x.

median (x, dim) returns the median along the operating dimension dim of x. For dim greater than ndims (x), then m = x.

median (x, vecdim) returns the median over the dimensions specified in the vector vecdim. For example, if x is a 2-by-3-by-4 array, then median (x, [1 2]) returns a 1-by-1-by-4 array. Each element of the output array is the median of the elements on the corresponding page of x. If vecdim indexes all dimensions of x, then it is equivalent to median (x, "all"). Any dimension in vecdim greater than ndims (x) is ignored.

median (x, "all") returns the median of all the elements in x. The optional flag "all" cannot be used together with dim or vecdim input arguments.

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.

median (…, nanflag) specifies whether to exclude NaN values from the calculation, using any of the input argument combinations in previous syntaxes. By default, NaN values are included in the calculation (nanflag has the value "includenan"). To exclude NaN values, set the value of nanflag to "omitnan".

See also: mean, mode

Source Code: median