Main Content

nanvar

(Not recommended) Variance, ignoring NaN values

nanvar is not recommended. Use the MATLAB® function var instead. With the var function, you can specify whether to include or omit NaN values for the calculation. For more information, see Compatibility Considerations.

Description

example

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.

nanvar 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.

y = nanvar(X,w) computes the variance of X according to the weighting scheme w. When w is 0 (default), X is normalized by n – 1, where n is the number of non-NaN observations. When w is 1, w is normalized by the number of non-NaN observations. Otherwise, w can be a weight vector containing nonnegative elements. The length of w must equal the length of the dimension over which nanvar operates. Elements of X corresponding to NaN values of w are ignored.

y = nanvar(X,w,'all') returns the variance over all elements of X when w = 0 or w = 1. The nanvar function computes the variance after removing NaN values.

y = nanvar(X,w,dim) returns the variance along the operating dimension dim of X.

example

y = nanvar(X,w,vecdim) returns the variance over the dimensions specified in the vector vecdim, computed after removing NaN values. 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-4 array, then nanvar(X,[],[1 2]) returns a 1-by-1-by-4 array. Each element of the output array is the variance of the elements on the corresponding page of X. This syntax is supported when w = 0 or w = 1.

Examples

collapse all

Find the column variances 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 = nanvar(X)
y = 1×3

    0.5000    8.0000       NaN

Find the variance of a multidimensional array over multiple dimensions.

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

X = reshape(1:24,[3 4 2]);
X([8:10 18]) = NaN
X = 
X(:,:,1) =

     1     4     7   NaN
     2     5   NaN    11
     3     6   NaN    12


X(:,:,2) =

    13    16    19    22
    14    17    20    23
    15   NaN    21    24

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

ypage = nanvar(X,0,[1 2])
ypage = 
ypage(:,:,1) =

   14.5000


ypage(:,:,2) =

   14.2727

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

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

ycol = nanvar(X,0,[1 3])
ycol = 1×4

   44.0000   40.3000   42.9167   40.3000

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

Extended Capabilities

Version History

Introduced before R2006a

collapse all

R2020b: nanvar is not recommended

nanvar is not recommended. Use the MATLAB function var instead. There are no plans to remove nanvar.

To update your code, change instances of the function name nanvar to var. Then specify the 'omitnan' option for the nanflag input argument.

var offers more extended capabilities for supporting tall arrays, GPU arrays, distribution arrays, C/C++ code generation, and GPU code generation.

See Also

|