This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Covariance ignoring NaN values


Y = nancov(X)
Y = nancov(X1,X2)
Y = nancov(...,1)
Y = nancov(...,'pairwise')


Y = nancov(X) is the covariance cov of X, computed after removing observations with NaN values.

For vectors x, nancov(x) is the sample variance of the remaining elements, once NaN values are removed. For matrices X, nancov(X) is the sample covariance of the remaining observations, once observations (rows) containing any NaN values are removed.

Y = nancov(X1,X2), where X1 and X2 are matrices with the same number of elements, is equivalent to nancov(X), where X = [X1(:) X2(:)].

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, nancov normalizes Y by either n – 1 or n , depending on whether n > 1 or n = 1, respectively. To specify normalization by n, use Y = nancov(...,1).

Y = nancov(...,'pairwise') computes Y(i,j) using rows with no NaN values in columns i or j. The result Y may not be a positive definite matrix.


Generate random data for two variables (columns) with random missing values:

X = rand(10,2);
p = randperm(numel(X));
X(p(1:5)) = NaN
X =
    0.8147    0.1576
       NaN       NaN
    0.1270    0.9572
    0.9134       NaN
    0.6324       NaN
    0.0975    0.1419
    0.2785    0.4218
    0.5469    0.9157
    0.9575    0.7922
    0.9649       NaN

Establish a correlation between a third variable and the other two variables:

X(:,3) = sum(X,2)
X =
    0.8147    0.1576    0.9723
       NaN       NaN       NaN
    0.1270    0.9572    1.0842
    0.9134       NaN       NaN
    0.6324       NaN       NaN
    0.0975    0.1419    0.2394
    0.2785    0.4218    0.7003
    0.5469    0.9157    1.4626
    0.9575    0.7922    1.7497
    0.9649       NaN       NaN

Compute the covariance matrix for the three variables after removing observations (rows) with NaN values:

Y = nancov(X)
Y =
    0.1311    0.0096    0.1407
    0.0096    0.1388    0.1483
    0.1407    0.1483    0.2890

Alternative Functionality

Instead of using nancov, you can use the MATLAB® function cov with the input argument nanflag. For example, instead of nancov(X), use cov(X,'omitrows'), and instead of nancov(X,'pairwise'), use cov(X,'partialrows').

Extended Capabilities

See Also

| | |

Introduced before R2006a