C = cov(A)

  • It returns the covariance of array A.
  • If A is a scalar, then it returns 0.
  • If A is a vector, then it returns the variance of vector A.

                              

  • If A is a matrix, then it considers each column as a random variable and returns the covariance matrix of matrix A.

Note: disp (x) displays the value of variable X without printing the variable name. Another way to display a variable is to type its name, which displays a leading “X =” before the value. If a variable contains an empty array, disp returns without displaying anything.

Example 1:

Matlab

% Input vector
A = [1 3 4];
disp("Vector :");
disp(A);
 
% Variance of vector A
C = cov(A);
disp("Variance :");
disp(C);

                    

 
 

Output :


 


 

Example 2:


 

Matlab

% Input vector
A = [2 7 1;
     3 5 1
     4 1 2];
disp("Matrix :");
disp(A);
 
% Covariance of matrix A
C = cov(A);
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:

  1. C = cov(A)
  2. C = cov(A,B)
  3. C = cov(___,w)
  4. C = cov(___,nanflag)

Similar Reads

C = cov(A)

It returns the covariance of array A.If A is a scalar, then it returns 0.If A is a vector, then it returns the variance of vector A....

C = cov(A,B)

...

C = cov(___,w)

...

C = cov(___,nanflag)

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....