Esta página aún no se ha traducido para esta versión. Puede ver la versión más reciente de esta página en inglés.


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').

Capacidades ampliadas

Consulte también

| | |

Introducido antes de R2006a