Bird
Raised Fist0
MATLABdata~10 mins

Correlation and covariance in MATLAB - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Correlation and covariance
Start with two data vectors X and Y
↓
Calculate mean of X and Y
↓
Compute deviations: X - mean(X), Y - mean(Y)
↓
Calculate covariance: mean of product of deviations
↓
Calculate standard deviations of X and Y
↓
Calculate correlation: covariance divided by product of std devs
↓
Output covariance and correlation values
This flow shows how MATLAB calculates covariance and correlation step-by-step from two data vectors.
Execution Sample
MATLAB
X = [1, 2, 3, 4, 5];
Y = [2, 4, 6, 8, 10];
covXY = cov(X, Y);
corrXY = corrcoef(X, Y);
This code calculates covariance and correlation between two simple vectors X and Y.
Execution Table
StepActionValue/CalculationResult
1Input vectorsX = [1 2 3 4 5], Y = [2 4 6 8 10]X and Y set
2Calculate meanmean(X) = 3, mean(Y) = 6Means computed
3Calculate deviationsX - mean(X) = [-2 -1 0 1 2], Y - mean(Y) = [-4 -2 0 2 4]Deviations found
4Calculate covariancesum((X-mean(X)) .* (Y-mean(Y))) / (length(X)-1)5
5Calculate std devstd(X) = 1.58, std(Y) = 3.16Standard deviations found
6Calculate correlationcovariance / (std(X)*std(Y)) = 5 / (1.58*3.16)1 (perfect correlation)
7OutputcovXY and corrXY matricesCovariance and correlation matrices returned
💡 All steps complete, covariance and correlation calculated successfully
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6Final
X[1 2 3 4 5][1 2 3 4 5][1 2 3 4 5][1 2 3 4 5][1 2 3 4 5][1 2 3 4 5][1 2 3 4 5]
Y[2 4 6 8 10][2 4 6 8 10][2 4 6 8 10][2 4 6 8 10][2 4 6 8 10][2 4 6 8 10][2 4 6 8 10]
mean(X)N/A333333
mean(Y)N/A666666
X - mean(X)N/AN/A[-2 -1 0 1 2][-2 -1 0 1 2][-2 -1 0 1 2][-2 -1 0 1 2][-2 -1 0 1 2]
Y - mean(Y)N/AN/A[-4 -2 0 2 4][-4 -2 0 2 4][-4 -2 0 2 4][-4 -2 0 2 4][-4 -2 0 2 4]
covarianceN/AN/AN/A5555
std(X)N/AN/AN/AN/A1.581.581.58
std(Y)N/AN/AN/AN/A3.163.163.16
correlationN/AN/AN/AN/AN/A11
Key Moments - 3 Insights
Why is the correlation exactly 1 in this example?
Because Y is exactly 2 times X, their deviations are perfectly proportional, so correlation is 1 as shown in step 6 of the execution_table.
Why do we subtract the mean before calculating covariance?
Subtracting the mean centers the data around zero, so covariance measures how X and Y vary together relative to their averages, as shown in step 3.
What is the difference between covariance and correlation?
Covariance shows joint variability in original units (step 4), while correlation normalizes it to a scale from -1 to 1 by dividing by standard deviations (step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the covariance value calculated?
A1
B5
C10
D0
💡 Hint
Refer to the 'Calculate covariance' row in the execution_table where the result is 5.
At which step in the execution_table do we calculate the standard deviations?
AStep 3
BStep 6
CStep 5
DStep 2
💡 Hint
Look for the row labeled 'Calculate std dev' in the execution_table.
If the vector Y was changed to [3 6 9 12 15], how would the correlation at step 6 change?
AIt would remain 1
BIt would become 0
CIt would become negative
DIt would be less than 1 but positive
💡 Hint
Since Y is still exactly proportional to X (multiplied by 3), correlation remains perfect as in step 6.
Concept Snapshot
Correlation and covariance in MATLAB:
- Use cov(X,Y) to get covariance matrix
- Use corrcoef(X,Y) to get correlation matrix
- Covariance measures joint variability
- Correlation normalizes covariance to [-1,1]
- Subtract means before calculating covariance
- Perfect linear relation gives correlation = 1
Full Transcript
This visual execution trace shows how MATLAB calculates covariance and correlation between two data vectors. First, it takes input vectors X and Y. Then it calculates their means. Next, it finds deviations by subtracting means from each element. Using these deviations, it calculates covariance as the average product of deviations. It also calculates standard deviations of X and Y. Finally, it computes correlation by dividing covariance by the product of standard deviations. The example vectors X = [1 2 3 4 5] and Y = [2 4 6 8 10] have perfect linear relation, so correlation is 1. The trace shows step-by-step values and variable changes, helping beginners understand how these statistics are computed in MATLAB.

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