Statistics: median

### 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.} $$ where N is the number of elements of x.

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.

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

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, mad, mode

Source Code: median