Main Content

nanmedian

R2026b

(Not recommended) Median, ignoring NaN values

nanmedian is not recommended. Use the MATLAB® function median instead. With the median function, you can specify whether to include or omit NaN values for the calculation. For more information, see Version History.

Description

y = nanmedian(X) returns the median of the elements of X, computed after removing all NaN values.

  • If X is a vector, then nanmedian(X) is the median of all the non-NaN elements of X.

  • If X is a matrix, then nanmedian(X) is a row vector of column medians, computed after removing NaN values.

  • If X is a multidimensional array, then nanmedian operates along the first nonsingleton dimension of X. The size of this dimension becomes 1 while the sizes of all other dimensions remain the same. nanmedian removes all NaN values.

example

y = nanmedian(X,"all") returns the median of all elements of X, computed after removing NaN values.

y = nanmedian(X,dim) returns the median along the operating dimension dim of X, computed after removing NaN values.

y = nanmedian(X,vecdim) returns the median over the dimensions specified in the vector vecdim. The function computes the medians after removing NaN values. For example, if X is a matrix, then nanmedian(X,[1 2]) is the median of all non-NaN elements of X, because every element of a matrix is contained in the array slice defined by dimensions 1 and 2.

example

Examples

collapse all

Find the column medians for matrix data with missing values.

X = magic(3);
X([1 6:9]) = NaN
X = 3×3

   NaN     1   NaN
     3     5   NaN
     4   NaN   NaN

y = nanmedian(X)
y = 1×3

    3.5000    3.0000       NaN

Find the median of a multidimensional array over multiple dimensions.

Create a 3-by-5-by-2 array X with some missing values.

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

     1     4     7   NaN    13
     2     5     8   NaN    14
     3     6     9   NaN    15


X(:,:,2) =

    16    19    22   NaN    28
    17    20    23    26    29
    18    21    24    27    30

Find the median of each page of X by specifying dimensions 1 and 2 as the operating dimensions.

ypage = nanmedian(X,[1 2])
ypage = 
ypage(:,:,1) =

    6.5000


ypage(:,:,2) =

   22.5000

For example, ypage(1,1,1) is the median of the non-NaN elements in X(:,:,1).

Find the median of the elements in each X(:,i,:) slice by specifying dimensions 1 and 3 as the operating dimensions.

ycol = nanmedian(X,[1 3])
ycol = 1×5

    9.5000   12.5000   15.5000   26.5000   21.5000

For example, ycol(4) is the median of the non-NaN elements in X(:,4,:).

Input Arguments

collapse all

Input data, specified as a scalar, vector, matrix, or multidimensional array.

If X is an empty array, then nanmedian(X) is NaN.

Dimension to operate along, specified as a positive integer scalar. If you do not specify a value, then the default value is the first array dimension whose size does not equal 1.

dim indicates the dimension whose length reduces to 1. size(y,dim) is 1 while the sizes of all other dimensions remain the same.

Consider a two-dimensional array X:

  • If dim is equal to 1, then nanmedian(X,1) returns a row vector containing the median for each column.

  • If dim is equal to 2, then nanmedian(X,2) returns a column vector containing the median for each row.

If dim is greater than ndims(X) or if size(X,dim) is 1, then nanmedian returns X.

Vector of dimensions, specified as a positive integer vector. Each element of vecdim represents a dimension of the input array X. The output y has length 1 in the specified operating dimensions. The other dimension lengths are the same for X and y.

For example, if X is a 2-by-3-by-3 array, then nanmedian(X,[1 2]) returns a 1-by-1-by-3 array. Each element of the output is the median of the elements on the corresponding page of X.

Mapping of input dimension of 2-by-3-by-3 to output dimension of 1-by-1-by-3

Output Arguments

collapse all

Median values, returned as a scalar, vector, matrix, or multidimensional array.

Extended Capabilities

expand all

Version History

Introduced before R2006a

collapse all

See Also

|