Categories &

Functions List

Function Reference: nanvar

statistics: v = nanvar (x)
statistics: v = nanvar (x, w)
statistics: v = nanvar (x, w, 'all')
statistics: v = nanvar (x, w, dim)
statistics: v = nanvar (x, w, vecdim)

Compute the variance while ignoring NaN values.

v = nanvar (x) returns the variance 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 variances is returned. If x is a multidimensional array, nanvar operates along the first nonsingleton dimension. If a dimension contains fewer than two non-NaN values, the variance is returned as 0 for a single value and as NaN when all values are NaN.

v = nanvar (x, w) specifies the normalization. When w is 0 (default), the variance is normalized by N-1, where N is the number of non-NaN observations. When w is 1, it is normalized by N. w may also be a vector of nonnegative weights whose length matches the operating dimension, in which case the weighted variance normalized by the sum of the weights is returned.

v = nanvar (x, w, 'all') returns the variance of all elements of x, after removing NaN values. Use an empty value, w = [], to pass the default normalization.

v = nanvar (x, w, dim) operates along the dimension dim of x.

v = nanvar (x, w, vecdim) returns the variance over the dimensions specified in the vector vecdim. A weight vector is not supported together with 'all' or vecdim. Any dimension in vecdim greater than ndims (x) is ignored.

See also: var, nanstd, nanmean, nansum

Source Code: nanvar

Find the column variances for a matrix with missing values.

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

   NaN     1   NaN
     3     5   NaN
     4   NaN   NaN
 v = nanvar (x)
v =

   0.5000   8.0000      NaN

Find the row variances, normalized by N instead of N-1.

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

   NaN     1   NaN
     3     5   NaN
     4   NaN   NaN
 v = nanvar (x, 1, 2)
v =

   0
   1
   0