Correlation and covariance in MATLAB - Mini Project: Build & Apply
dataX and dataY with these exact values: dataX = [2, 4, 6, 8, 10] and dataY = [1, 3, 5, 7, 9].Use square brackets to create vectors in MATLAB.
n that stores the number of elements in dataX using the length function.Use length(dataX) to find how many elements are in dataX.
dataX and dataY using cov([dataX; dataY]') and store it in covMatrix. Then calculate the correlation matrix using corrcoef(dataX, dataY) and store it in corrMatrix.Use MATLAB's cov and corrcoef functions to calculate covariance and correlation matrices.
disp to print the text 'Covariance matrix:' and then display covMatrix. Then print 'Correlation matrix:' and display corrMatrix.Use disp twice to show the labels and matrices.
Practice
What does the MATLAB function cov compute?
Solution
Step 1: Understand the purpose of
Thecovcovfunction calculates the covariance matrix, which shows how two variables change together.Step 2: Differentiate covariance from correlation
Correlation measures strength and direction, butcovspecifically returns covariance values.Final Answer:
Covariance matrix showing how two variables vary together -> Option BQuick Check:
cov = covariance matrix [OK]
- Confusing covariance with correlation
- Thinking cov returns a single value instead of a matrix
- Mixing up cov with mean or std functions
Which of the following is the correct syntax to calculate the correlation matrix of variables X and Y in MATLAB?
?
Solution
Step 1: Identify MATLAB function for correlation
The correct MATLAB function to compute correlation coefficients iscorrcoef.Step 2: Check syntax correctness
The syntaxR = corrcoef(X, Y);correctly computes the correlation matrix betweenXandY.Final Answer:
R = corrcoef(X, Y); -> Option CQuick Check:
corrcoef syntax = R = corrcoef(X, Y) [OK]
- Using cov instead of corrcoef for correlation
- Using non-existent functions like correlation or corr
- Missing parentheses or semicolon
What is the output of the following MATLAB code?
X = [1 2 3]; Y = [4 5 6]; R = corrcoef(X, Y); disp(R);
Solution
Step 1: Understand the data vectors
VectorsXandYare perfectly linearly related: Y = X + 3.Step 2: Calculate correlation coefficient
Since they have a perfect positive linear relationship, correlation coefficient is 1.Final Answer:
[[1 1]; [1 1]] -> Option DQuick Check:
Perfect positive correlation = 1 [OK]
- Assuming correlation less than 1 for linear relation
- Confusing covariance with correlation values
- Misreading matrix output format
Identify the error in this MATLAB code snippet:
X = [1 2 3]; Y = [4 5 6]; R = corrcoef(X Y); disp(R);
Solution
Step 1: Check function call syntax
The function callcorrcoef(X Y)is missing a comma betweenXandY.Step 2: Confirm correct syntax
The correct syntax iscorrcoef(X, Y)with a comma separating inputs.Final Answer:
Missing comma between X and Y in corrcoef function -> Option AQuick Check:
Function arguments need commas [OK]
- Omitting commas between function arguments
- Thinking vectors must be columns for corrcoef
- Believing disp cannot show matrices
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?
Solution
Step 1: Calculate covariance and correlation matrices
Usingcov(A, B)andcorrcoef(A, B)returns 2x2 matrices with variances/covariances and correlations.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.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 displayscovAB(2,1)andcorrAB(2,1), confirming the relationship.Final Answer:
covAB = cov(A, B); corrAB = corrcoef(A, B); disp(covAB(2,1)); disp(corrAB(2,1)); -> Option AQuick Check:
Use off-diagonal elements for covariance and correlation [OK]
- Displaying entire matrix instead of specific covariance/correlation
- Using diagonal elements which are variances
- Confusing matrix indices
Ready to prove it?
Pass the test and this pattern moves unknown
