| by Arround The Web | No comments

## How to Calculate Covariance in MATLAB?

Covariance is a statistical operation widely used in statistics and data science. This is a complicated operation that can be manually performed on small data sets but for large data sets, performing this operation manually is an impractical approach. However, we can perform this operation on large data sets using the built-in MATLAB function called cov().

This guide will teach us how to compute covariance in MATLAB.

## What is Covariance?

Covariance is a statistical operation that enables us to identify the strength of the core relation between two or more random variables. It’s utilized for finding the effect of change from one variable to the other variable. This quantity for the two variables A and B can be defined as:

Where E(A) and E(B) represent the random variables A and B’s mean or expected values.

The covariance matrix corresponding to two random variables A and B can be defined as:

## How to Calculate Covariance in MATLAB?

MATLAB allows us to compute the covariance of two or more random variables using the built-in cov() function. This function accepts two or more random variables or an array of random variables as a mandatory argument and returns a scalar value that is the calculated covariance of the given random variables.

### Syntax

The cov() function’s syntax is given below:

C = cov(A)

C = cov(A,B)

Here:

The function C = cov(A) yields to compute the covariance.

• If A represents a vector of observations, C will be a scalar value covariance.
• If A represents a matrix whose columns represent the random variables and rows represent observations, C will be a matrix having covariance of the columns along the diagonals.
• If A represents a scalar value, the function will return zero.
• If A is an empty vector, the function will return NaN.

The function C = cov(A,B) yields to compute the covariance of the given random variables.

• If A and B represent two vectors of observations having the same length, C will be a two-by-two covariance matrix.
• If A and B represent scalar values, the function will return a two-by-two block of zeros.
• If A and B represent empty vectors, the function will return a two-by-two block of NaN.

## Examples

Consider some examples to learn how to implement the cov() function for finding covariance in MATLAB.

### Example 1: How to Calculate Covariance in MATLAB Using cov(A) Function?

This example computes the covariance of the given scalar, vector, and matrix using the cov(A) function in MATLAB.

a = 10;

vect = [2 4 6 8];

A = magic(2);

C_scalar = cov(a)

C_vect = cov(vect)

C_mat = cov(A)

### Example 2: How to Calculate Covariance in MATLAB Using cov(A,B) Function?

The given example calculates the covariance of the given two vectors A and B using the cov(A,B) function in MATLAB and returns a covariance matrix corresponding to A and B.

A = rand(1,4);

B = randn(1,4);

C = cov(A,B)

## Conclusion

Covariance is a statistical operation used for finding the correlation between two or more random variables. We can compute covariance in MATLAB using the built-in function cov(). This function accepts two or more random variables or an array of random variables as arguments and returns their calculated covariance. This guide provided a detailed discussion on how to calculate covariance in MATLAB using the cov() function. The examples provided are easy and help users learn the use of the cov() function in MATLAB.

Source: linuxhint.com