Documentation

Syntax

Description

example

C = cov(A) returns the covariance.

  • If A is a vector of observations, C is the scalar-valued variance.

  • If A is a matrix whose columns represent random variables and whose rows represent observations, C is the covariance matrix with the corresponding column variances along the diagonal.

  • C is normalized by the number of observations-1. If there is only one observation, it is normalized by 1.

  • If A is a scalar, cov(A) returns 0. If A is an empty array, cov(A)returns NaN.

example

C = cov(A,B) returns the covariance between two random variables A and B.

  • If A and B are vectors of observations with equal length, cov(A,B) is the 2-by-2 covariance matrix.

  • If A and B are matrices of observations, cov(A,B) treats A and B as vectors and is equivalent to cov(A(:),B(:)). A and B must have equal size.

  • If A and B are scalars, cov(A,B) returns a 2-by-2 block of zeros. If A and B are empty arrays, cov(A,B) returns a 2-by-2 block of NaN.

example

C = cov(___,w) specifies the normalization weight for any of the previous syntaxes. When w = 0 (default), C is normalized by the number of observations-1. When w = 1, it is normalized by the number of observations.

example

C = cov(___,nanflag) specifies a condition for omitting NaN values from the calculation for any of the previous syntaxes. For example, cov(A,'omitrows') will omit any rows of A with one or more NaN elements.

Examples

collapse all

Covariance of Matrix

Create a 3-by-4 matrix and compute its covariance.

A = [5 0 3 7; 1 -5 7 3; 4 9 8 10];
C = cov(A)
C =

    4.3333    8.8333   -3.0000    5.6667
    8.8333   50.3333    6.5000   24.1667
   -3.0000    6.5000    7.0000    1.0000
    5.6667   24.1667    1.0000   12.3333

Since the number of columns of A is 4, the result is a 4-by-4 matrix.

Covariance of Two Vectors

Create two vectors and compute their 2-by-2 covariance matrix.

A = [3 6 4];
B = [7 12 -9];
cov(A,B)
ans =

    2.3333    6.8333
    6.8333  120.3333

Covariance of Two Matrices

Create two matrices of the same size and compute their 2-by-2 covariance.

A = [2 0 -9; 3 4 1];
B = [5 2 6; -4 4 9];
cov(A,B)
ans =

   22.1667   -6.9333
   -6.9333   19.4667

Specify Normalization Weight

Create a matrix and compute the covariance normalized by the number of rows.

A = [1 3 -7; 3 9 2; -5 4 6];
C = cov(A,1)
C =

   11.5556    5.1111  -10.2222
    5.1111    6.8889    5.2222
  -10.2222    5.2222   29.5556

Covariance Excluding NaN

Create a matrix and compute its covariance, excluding any rows containing NaN values.

A = [1.77 -0.005 3.98; NaN -2.95 NaN; 2.54 0.19 1.01]
A =

    1.7700   -0.0050    3.9800
       NaN   -2.9500       NaN
    2.5400    0.1900    1.0100

C = cov(A,'omitrows')
C =

    0.2964    0.0751   -1.1435
    0.0751    0.0190   -0.2896
   -1.1435   -0.2896    4.4104

Input Arguments

collapse all

A — Input arrayvector | matrix

Input array, specified as a vector or matrix.

Data Types: single | double

B — Additional input arrayvector | matrix

Additional input matrix, specified as a vector or matrix. B must be the same size as A.

Data Types: single | double

w — Normalization weight0 (default) | 1

Normalization weight, specified as one of these values:

  • 0 — The output is normalized by the number of observations-1. If there is only one observation, it is normalized by 1.

  • 1 — The output is normalized by the number of observations.

Data Types: single | double

nanflagNaN conditionincludenan' (default) | ‘omitrows' | ‘partialrows'

NaN condition, specified as one of these values:

  • includenan' — include all NaN values in the input prior to computing the covariance.

  • omitrows' — omit any row of input containing one or more NaN values prior to computing the covariance.

  • partialrows' — omit rows containing NaN only on a pair-wise basis for each two-column covariance calculation.

Data Types: char

Output Arguments

collapse all

C — Covariancescalar | matrix

Covariance, specified as a scalar or matrix.

  • For single matrix input, C has size [size(A,2) size(A,2)] based on the number of random variables (columns) represented by A. The variances of the columns are along the diagonal. If A is a row or column vector, C is the scalar-valued variance.

  • For two-vector or two-matrix input, C is the 2-by-2 covariance matrix between the two random variables. The variances are along the diagonal of C.

More About

collapse all

Covariance

For two random variable vectors A and B, the covariance is defined as

cov(A,B)=1N1i=1N(AiμA)*(BiμB)

where μA is the mean of A, μB is the mean of B, and * denotes the complex conjugate.

The covariance matrix of two random variables is the matrix of pair-wise covariance calculations between each variable,

C=(cov(A,A)cov(A,B)cov(B,A)cov(B,B)).

For a matrix A whose columns are each a random variable made up of observations, the covariance matrix is the pair-wise covariance calculation between each column combination. In other words, C(i,j)=cov(A(:,i),A(:,j)).

Variance

For a random variable vector A made up of N scalar observations, the variance is defined as

V=1N1i=1N|Aiμ|2

where μ is the mean of A,

μ=1Ni=1NAi.

Some definitions of variance use a normalization factor of N instead of N-1, which can be specified by setting w to 1. In either case, the mean is assumed to have the usual normalization factor N.

See Also

| | | | | |

Introduced before R2006a

Was this topic helpful?