# cov

## Purpose

Covariance matrix.

## Synopsis

    C = cov(X)
    C = cov(x,y)

## Description

`C = cov(X)`, where `X` is a vector,`` returns a scalar containing the variance. `C = cov(X`), where `X` is a matrix with each row an observation and each column a variable, returns the covariance matrix. `diag(cov(X))` is a vector of variances for each column, and `sqrt(diag(cov(X)))` is a vector of standard deviations.

`cov(X)` is the 0-th lag of the covariance function, that is, the 0-th lag of `xcov(X)/(n-1)` packed into a square array.

`cov(x,y)`, where `x` and `y` are column vectors of equal length, is equivalent to `cov([x y])`.

`cov` removes the mean from each column before calculating the results.

## Algorithm

The basic algorithm for `cov` is

    [n,p] = size(X);
    X = X-ones(n,1)*mean(X);
    Y = X'*X/(n-1);

## See Also

`corrcoef`, `mean`, `std`

`xcorr`,`xcov` in the Signal Processing Toolbox

(c) Copyright 1994 by The MathWorks, Inc.