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:
- C = cov(A)
- C = cov(A,B)
- C = cov(___,w)
- C = cov(___,nanflag)