Categories &

Functions List

Function Reference: nanmedian

statistics: m = nanmedian (x)
statistics: m = nanmedian (x, 'all')
statistics: m = nanmedian (x, dim)
statistics: m = nanmedian (x, vecdim)

Compute the median while ignoring NaN values.

m = nanmedian (x) returns the median of x, after removing NaN values. If x is a vector, a scalar value is returned. If x is a matrix, a row vector of column medians is returned. If x is a multidimensional array, nanmedian operates along the first nonsingleton dimension. If all values along a dimension are NaN, the median is returned as NaN.

m = nanmedian (x, 'all') returns the median of all elements of x, after removing NaN values. It is the equivalent of nanmedian (x(:)).

m = nanmedian (x, dim) operates along the dimension dim of x.

m = nanmedian (x, vecdim) returns the median over the dimensions specified in the vector vecdim. Each element of vecdim represents a dimension of the input array x and the output m has length 1 in the specified operating dimensions. If vecdim indexes all dimensions of x, then it is equivalent to nanmedian (x, 'all'). Any dimension in vecdim greater than ndims (x) is ignored.

See also: median, nanmean, nansum

Source Code: nanmedian

Find the column medians for a matrix with missing values.

 x = magic (3);
 x([1, 6:9]) = NaN
x =

   NaN     1   NaN
     3     5   NaN
     4   NaN   NaN
 m = nanmedian (x)
m =

   3.5000   3.0000      NaN

Find the median of all elements, ignoring missing values.

 x = reshape (1:30, [2, 5, 3]);
 x([10:12, 25]) = NaN
x =

ans(:,:,1) =

     1     3     5     7     9
     2     4     6     8   NaN

ans(:,:,2) =

   NaN    13    15    17    19
   NaN    14    16    18    20

ans(:,:,3) =

    21    23   NaN    27    29
    22    24    26    28    30
 m = nanmedian (x, 'all')
m = 16.500