C = cov(___,nanflag)

  • It returns the covariance of the input array by considering the nanflag.
  • If nanflag = ‘includenan’, then it considers NaN values in array.
  • If nanflag = ‘omitrows’, then it omits the rows with at least one NaN value in the array.


 

Example:


 

Matlab

% Input vector
A = [3.2 -1.005 2.98;
     NaN  -6.75  NaN;
     5.37  0.19  1]
disp("Matrix :");
disp(A);
 
% Variance of matrix A
C = cov(A,'includenan');
disp("Variance matrix including NaN:");
disp(C);
 
 
% Variance of matrix A
C = cov(A,'omitrows');
disp("Variance matrix omitting NaN:");
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....