You are here : matlabSignal Processingcov

# cov() - Signal Processing

```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.exampleC = 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.exampleC = 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.exampleC = 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.```

### Syntax

`C = cov(A) exampleC = cov(A,B) exampleC = cov(___,w) exampleC = cov(___,nanflag) example`

### Example

```Covariance of MatrixOpen This ExampleCreate 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 VectorsOpen This ExampleCreate 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 MatricesOpen This ExampleCreate 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 WeightOpen This ExampleCreate 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 NaNOpen This ExampleCreate 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```