A is a vector of observations,
the scalar-valued variance.
A is a matrix whose columns
represent random variables and whose rows represent observations,
the covariance matrix with the corresponding column variances along
C is normalized by the number of
-1. If there is only one observation,
it is normalized by 1.
A is a scalar,
A is an empty array,
vectors of observations with equal length,
2 covariance matrix.
matrices of observations,
vectors and is equivalent to
have equal size.
cov(A,B) returns a
of zeros. If
B are empty
cov(A,B) returns a
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×4 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.
Create two vectors and compute their 2-by-2 covariance matrix.
A = [3 6 4]; B = [7 12 -9]; cov(A,B)
ans = 2×2 2.3333 6.8333 6.8333 120.3333
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 = 2×2 22.1667 -6.9333 -6.9333 19.4667
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 = 3×3 11.5556 5.1111 -10.2222 5.1111 6.8889 5.2222 -10.2222 5.2222 29.5556
Create a matrix and compute its covariance, excluding any rows containing
A = [1.77 -0.005 3.98; NaN -2.95 NaN; 2.54 0.19 1.01]
A = 3×3 1.7700 -0.0050 3.9800 NaN -2.9500 NaN 2.5400 0.1900 1.0100
C = cov(A,'omitrows')
C = 3×3 0.2964 0.0751 -1.1435 0.0751 0.0190 -0.2896 -1.1435 -0.2896 4.4104
A— Input array
Input array, specified as a vector or matrix.
B— Additional input array
Additional input matrix, specified as a vector or matrix.
be the same size as
w— Normalization weight
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.
NaN condition, specified as one of these
'includenan' — include all
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
NaN only on a pairwise basis for each
two-column covariance calculation.
Covariance, specified as a scalar or matrix.
For single matrix input,
[size(A,2) size(A,2)] based on the number
of random variables (columns) represented by
The variances of the columns are along the diagonal. If
a row or column vector,
C is the scalar-valued
For two-vector or two-matrix input,
2 covariance matrix
between the two random variables. The variances are along the diagonal
For two random variable vectors A and B, the covariance is defined as
where μA is
the mean of A, μB is
the mean of B, and
the complex conjugate.
The covariance matrix of two random variables is the matrix of pairwise covariance calculations between each variable,
For a matrix
columns are each a random variable made up of observations, the covariance
matrix is the pairwise covariance calculation between each column
combination. In other words, .
For a random variable vector A made up of N scalar observations, the variance is defined as
where μ is the mean of A,
Some definitions of
variance use a normalization factor of N instead
of N-1, which can be specified by setting
In either case, the mean is assumed to have the usual normalization
This function supports tall arrays with the limitations:
B must be tall arrays of the same size,
even if both are vectors.
'partialrows' option is not supported.
For more information, see Tall Arrays for Out-of-Memory Data.
Usage notes and limitations:
If the input is variable-size and is
run time, returns
See Variable-Sizing Restrictions for Code Generation of Toolbox Functions (MATLAB Coder).
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).