Variance, ignoring `NaN`

values

`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`

.

`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`

.

Instead of using `nanvar`

, you can use the MATLAB^{®} function `var`

with the input argument
`nanflag`

specified as the value `'omitnan'`

.