Variance, ignoring NaN values


y = nanvar(X)
y = nanvar(X,1)
y = nanvar(X,w)
y = nanvar(X,w,dim)


y = nanvar(X) is the variance var of X, computed after removing NaN values.

For vectors x, nanvar(x) is the sample variance of the remaining elements, once NaN values are removed. For matrices X, nanvar(X) is a row vector of column sample variances, once NaN values are removed. For multidimensional arrays X, nanvar operates along the first nonsingleton dimension.

nancov removes the mean from each variable (column for matrix X) before calculating Y. If n is the number of remaining observations after removing observations with NaN values, nanvar normalizes y by either n – 1 or n , depending on whether n > 1 or n = 1, respectively. To specify normalization by n, use y = nanvar(X,1).

y = nanvar(X,w) computes the variance using the weight vector w. The length of w must equal the length of the dimension over which nanvar operates, and its elements must be nonnegative. Elements of X corresponding to NaN values of w are ignored.

y = nanvar(X,w,dim) takes the variance along the dimension dim of X. Set w to [] to use the default normalization by n – 1.


Find column standard deviations for data with missing values:

X = magic(3);
X([1 6:9]) = repmat(NaN,1,5)
X =
   NaN     1   NaN
     3     5   NaN
     4   NaN   NaN
y = nanvar(X)
y =
   0.5000  8.0000  NaN

See Also

| | |

Was this topic helpful?