cov
Covariance of vectors or of the columns of a matrix.
Category: Data Import and Analysis > Descriptive Statistics
Syntax
C = cov(A)
Covariance matrix of the columns of A; for a vector, its variance.
C = cov(x, y)
Covariance matrix of the two variables, 2 by 2.
Description
cov normalizes by N-1, where N is the number of observations, so the diagonal equals var of each column. A positive off-diagonal value means the two variables tend to grow together, a negative one means that one grows as the other falls.
The covariance depends on the units of the data. To compare strengths of association, use corrcoef, which rescales to the range -1 to 1.
Input arguments
A- Vector or matrix with variables in columns.
x, y- Vectors with the same number of elements.
Output arguments
C- Covariance matrix. The diagonal holds the variances.
Example
Covariance matrix
x = [1 2 3 4];
y = [2 4 5 9];
C = cov(x, y)
Output
C = 2×2
1.6667 3.6667
3.6667 8.6667