Ais a vector of observations,
Cis the scalar-valued variance.
Ais a matrix whose columns represent random variables and whose rows represent observations,
Cis the covariance matrix with the corresponding column variances along the diagonal.
Ais a scalar,
Ais an empty array,
C is normalized by the number of
-1. If there is only one observation, it is
normalized by 1.
Bare vectors of observations with equal length,
Bare matrices of observations,
Bas vectors and is equivalent to
Bmust be the same size.
2block of zeros. If
Bare empty arrays,
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×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.
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×2 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 = 2×2 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 = 3×3 11.5556 5.1111 -10.2222 5.1111 6.8889 5.2222 -10.2222 5.2222 29.5556
Covariance Excluding Missing Values
Create a matrix containing
A = [1.77 -0.005 3.98; NaN -2.95 NaN; 2.54 0.19 1.01];
Compute the covariance of the matrix, excluding rows that contain any
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
vector | matrix
Input array, specified as a vector or matrix.
B — Additional input array
vector | matrix
Additional input matrix, specified as a vector or matrix.
be the same size as
w — Normalization weight
0 (default) |
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.
nanflag — Missing value condition
"includemissing" (default) |
Missing value condition, specified as one of these values:
NaNvalues in the input arrays when computing the covariance.
"includenan"have the same behavior.
"omitrows"— Omit any rows of the input arrays containing one or more
NaNvalues when computing the covariance.
"partialrows"— Omit rows of the input arrays containing
NaNvalues only on a pairwise basis for each two-column covariance calculation.
C — Covariance
scalar | matrix
Covariance, returned 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
A. The variances of the columns are along the diagonal. If
Ais a row or column vector,
Cis the scalar-valued variance.
For two-vector or two-matrix input,
2covariance matrix between the two random variables. The variances are along the diagonal of
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 finite-length 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 you can specify by
1. In either case, the mean is
assumed to have the usual normalization factor N.
Calculate with arrays that have more rows than fit in memory.
This function supports tall arrays with the limitations:
Bmust 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.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
If the input is variable-size and is
at run time, the function returns
See Variable-Sizing Restrictions for Code Generation of Toolbox Functions (MATLAB Coder).
Run code in the background using MATLAB®
backgroundPool or accelerate code with Parallel Computing Toolbox™
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Accelerate code by running on a graphics processing unit (GPU) using Parallel Computing Toolbox™.
This function fully supports GPU arrays. For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).
Partition large arrays across the combined memory of your cluster using Parallel Computing Toolbox™.
This function fully supports distributed arrays. For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).
Version HistoryIntroduced before R2006a
R2023a: Specify missing value condition
Include missing values in the input arrays when computing the covariance by using
"includemissing" option. This option has the same behavior as