Categories &

Functions List

Function Reference: nanmean

statistics: s = nanmean (x)
statistics: s = nanmean (x, 'all')
statistics: s = nanmean (x, dim)
statistics: s = nanmean (x, vecdim)

Compute the mean while ignoring NaN values.

s = nanmean (x) returns the mean 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 means is returned. If x is a multidimensional array, nanmean operates along the first nonsingleton dimension. If all values along a dimesion are NaN, the mean is returned returned as NaN.

s = nanmean (x, 'all') returns the mean of all elements of x, after removing NaN values. It is the equivalent of nanmean (x(:)).

s = nanmean (x, dim) operates along the dimension dim of x.

s = nanmean (x, vecdim) returns the mean over the dimensions specified in the vector vecdim. Each element of vecdim represents a dimension of the input array x and the output s has length 1 in the specified operating dimensions. The lengths of the other dimensions are the same for x and y. For example, if x is a 2-by-3-by-4 array, then nanmean (x, [1 2]) returns a 1-by-1-by-4 array. Each element of the output array is the mean of the elements on the corresponding page of x. If vecdim indexes all dimensions of x, then it is equivalent to nanmean (x, 'all'). Any dimension in vecdim greater than ndims (x) is ignored.

nanmean primarily operates on single and double numeric types, since they support NaN values, while preserving the data type. Nevertheless, it can also operate on integer types by treating them as double types. To avoid overflow on very large int64 and uint64 values, use the mean function, which applies special handling for such cases.

See also: mean, nansum, nanmin, nanmax

Source Code: nanmean

Example: 1

 

 ## Find the column means for a matrix with missing values.,

 x = magic (3);
 x([1, 4, 7:9]) = NaN
 y = nanmean (x)

x =

   NaN   NaN   NaN
     3     5   NaN
     4     9   NaN

y =

   3.5000   7.0000      NaN

                    

Example: 2

 

 ## Find the row means for a matrix with missing values.,

 x = magic (3);
 x([1, 4, 7:9]) = NaN
 y = nanmean (x, 2)

x =

   NaN   NaN   NaN
     3     5   NaN
     4     9   NaN

y =

      NaN
   4.0000
   6.5000

                    

Example: 3

 

 ## Find the mean of all the values in a multidimensional array
 ## with missing values.

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

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

y = 15.654
                    

Example: 4

 

 ## Find the mean of a multidimensional array with missing values over
 ## multiple dimensions.

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

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

y =

   14.538
   16.769