Bird
Raised Fist0
MATLABdata~5 mins

Correlation and covariance in MATLAB

Choose your learning style10 modes available
Introduction

Correlation and covariance help us understand how two sets of numbers move together. They show if one goes up when the other goes up or down.

To find out if two variables like height and weight are related.
To check if sales and advertising spending move together.
To analyze if temperature and ice cream sales increase at the same time.
To see if two stock prices move in the same direction.
To understand relationships between exam scores in different subjects.
Syntax
MATLAB
C = cov(X)
R = corrcoef(X)

cov(X) calculates covariance matrix of data X.

corrcoef(X) calculates correlation matrix of data X.

Examples
Calculates covariance between columns of X.
MATLAB
X = [1 2 3; 4 5 6];
cov(X)
Calculates correlation between columns of X.
MATLAB
X = [1 2 3; 4 5 6];
corrcoef(X)
Calculates covariance between two vectors x and y.
MATLAB
x = [1 2 3 4 5];
y = [2 4 6 8 10];
cov(x',y')
Calculates correlation coefficient between x and y.
MATLAB
x = [1 2 3 4 5];
y = [2 4 6 8 10];
corrcoef(x,y)
Sample Program

This program calculates and shows covariance and correlation matrices for two simple vectors x and y.

MATLAB
x = [1 2 3 4 5];
y = [2 4 6 8 10];
C = cov(x',y');
R = corrcoef(x,y);

fprintf('Covariance matrix:\n');
disp(C);

fprintf('Correlation matrix:\n');
disp(R);
OutputSuccess
Important Notes

Covariance shows how two variables vary together but depends on units.

Correlation is scaled between -1 and 1, easy to compare.

Correlation of 1 means perfect positive relationship, -1 means perfect negative.

Summary

Covariance measures how two variables change together.

Correlation measures strength and direction of their relationship.

Use cov and corrcoef in MATLAB to find these.

Practice

(1/5)
1.

What does the MATLAB function cov compute?

easy
A. Standard deviation of a variable
B. Covariance matrix showing how two variables vary together
C. Mean value of a dataset
D. Correlation coefficient between two variables

Solution

  1. Step 1: Understand the purpose of cov

    The cov function calculates the covariance matrix, which shows how two variables change together.
  2. Step 2: Differentiate covariance from correlation

    Correlation measures strength and direction, but cov specifically returns covariance values.
  3. Final Answer:

    Covariance matrix showing how two variables vary together -> Option B
  4. Quick Check:

    cov = covariance matrix [OK]
Hint: Remember: cov = covariance matrix, corrcoef = correlation [OK]
Common Mistakes:
  • Confusing covariance with correlation
  • Thinking cov returns a single value instead of a matrix
  • Mixing up cov with mean or std functions
2.

Which of the following is the correct syntax to calculate the correlation matrix of variables X and Y in MATLAB?

?
easy
A. R = cov(X, Y);
B. R = correlation(X, Y);
C. R = corrcoef(X, Y);
D. R = corr(X, Y);

Solution

  1. Step 1: Identify MATLAB function for correlation

    The correct MATLAB function to compute correlation coefficients is corrcoef.
  2. Step 2: Check syntax correctness

    The syntax R = corrcoef(X, Y); correctly computes the correlation matrix between X and Y.
  3. Final Answer:

    R = corrcoef(X, Y); -> Option C
  4. Quick Check:

    corrcoef syntax = R = corrcoef(X, Y) [OK]
Hint: Use corrcoef for correlation, not cov [OK]
Common Mistakes:
  • Using cov instead of corrcoef for correlation
  • Using non-existent functions like correlation or corr
  • Missing parentheses or semicolon
3.

What is the output of the following MATLAB code?

X = [1 2 3];
Y = [4 5 6];
R = corrcoef(X, Y);
disp(R);
medium
A. [[1 0]; [0 1]]
B. [[1 0.5]; [0.5 1]]
C. [[1 -1]; [-1 1]]
D. [[1 1]; [1 1]]

Solution

  1. Step 1: Understand the data vectors

    Vectors X and Y are perfectly linearly related: Y = X + 3.
  2. Step 2: Calculate correlation coefficient

    Since they have a perfect positive linear relationship, correlation coefficient is 1.
  3. Final Answer:

    [[1 1]; [1 1]] -> Option D
  4. Quick Check:

    Perfect positive correlation = 1 [OK]
Hint: Perfect linear relation means correlation = 1 [OK]
Common Mistakes:
  • Assuming correlation less than 1 for linear relation
  • Confusing covariance with correlation values
  • Misreading matrix output format
4.

Identify the error in this MATLAB code snippet:

X = [1 2 3];
Y = [4 5 6];
R = corrcoef(X Y);
disp(R);
medium
A. Missing comma between X and Y in corrcoef function
B. Vectors X and Y must be column vectors
C. corrcoef cannot take two inputs
D. disp cannot display matrices

Solution

  1. Step 1: Check function call syntax

    The function call corrcoef(X Y) is missing a comma between X and Y.
  2. Step 2: Confirm correct syntax

    The correct syntax is corrcoef(X, Y) with a comma separating inputs.
  3. Final Answer:

    Missing comma between X and Y in corrcoef function -> Option A
  4. Quick Check:

    Function arguments need commas [OK]
Hint: Separate function arguments with commas [OK]
Common Mistakes:
  • Omitting commas between function arguments
  • Thinking vectors must be columns for corrcoef
  • Believing disp cannot show matrices
5.

You have two datasets A = [1 2 3 4] and B = [2 4 6 8]. You want to check if B is exactly twice A using correlation and covariance. Which MATLAB code snippet correctly confirms this?

hard
A. covAB = cov(A, B); corrAB = corrcoef(A, B); disp(covAB(2,1)); disp(corrAB(2,1));
B. covAB = cov(A, B); corrAB = corrcoef(A, B); disp(covAB); disp(corrAB);
C. covAB = cov(A, B); corrAB = corrcoef(A, B); disp(covAB(1,2)); disp(corrAB(1,2));
D. covAB = cov(A, B); corrAB = corrcoef(A, B); disp(covAB(1,1)); disp(corrAB(1,1));

Solution

  1. Step 1: Calculate covariance and correlation matrices

    Using cov(A, B) and corrcoef(A, B) returns 2x2 matrices with variances/covariances and correlations.
  2. Step 2: Extract covariance and correlation between A and B

    The off-diagonal elements (1,2) or (2,1) give covariance and correlation between A and B.
  3. Step 3: Confirm correct display of these values

    covAB = cov(A, B); corrAB = corrcoef(A, B); disp(covAB(2,1)); disp(corrAB(2,1)); correctly displays covAB(2,1) and corrAB(2,1), confirming the relationship.
  4. Final Answer:

    covAB = cov(A, B); corrAB = corrcoef(A, B); disp(covAB(2,1)); disp(corrAB(2,1)); -> Option A
  5. Quick Check:

    Use off-diagonal elements for covariance and correlation [OK]
Hint: Off-diagonal matrix elements show variable relationships [OK]
Common Mistakes:
  • Displaying entire matrix instead of specific covariance/correlation
  • Using diagonal elements which are variances
  • Confusing matrix indices
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard