C = cov(A,B)
- It returns the covariance matrix of arrays A and B.
- If A and B vectors, then it returns the covariance matrix of A and B.
- If A and B are matrices, then it considers them as vectors themselves by expanding the dimensions and returns the covariance matrix.
Example:
Matlab
% Input vector A = [3 5 7]; B = [-1 3 9]; disp( "Vector A:" ); disp(A); disp( "Vector B:" ); disp(B); % Covariance of vectors A,B C = cov(A,B); disp( "Covariance matrix :" ); disp(C); |
Output :
How to Calculate Covariance in MATLAB
Covariance is the measure of the strength of correlation between two or more random variables. Covariance of two random variables X and Y can be defined as:
Where E(X) and E(Y) are expectation or mean of random variables X and Y respectively.
The covariance matrix of two random variables A and B is defined as
MATLAB language allows users to calculate the covariance of random variables using cov() method. Different syntax of cov() method are:
- C = cov(A)
- C = cov(A,B)
- C = cov(___,w)
- C = cov(___,nanflag)