Bird
Raised Fist0
MATLABdata~20 mins

Correlation and covariance in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Correlation and Covariance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Calculate covariance matrix output
What is the output of the following MATLAB code snippet that calculates the covariance matrix of two variables?
MATLAB
X = [1 2 3 4 5; 2 4 6 8 10];
covMatrix = cov(X');
disp(covMatrix);
A[2.5 2.5; 5 5]
B[2.5 5; 5 10]
C[2.5 5; 2.5 5]
D[2.5 10; 5 20]
Attempts:
2 left
💡 Hint
Remember that cov(X') computes covariance between rows of X, so transpose is important.
❓ Predict Output
intermediate
2:00remaining
Correlation coefficient calculation
What is the output of this MATLAB code that calculates the correlation coefficient between two vectors?
MATLAB
A = [1 2 3 4 5];
B = [2 4 6 8 10];
r = corrcoef(A, B);
disp(r(1,2));
A0.5
B-1
C0
D1
Attempts:
2 left
💡 Hint
Check if B is a perfect linear multiple of A.
🔧 Debug
advanced
2:00remaining
Identify the error in covariance calculation
What error does this MATLAB code produce when trying to calculate covariance?
MATLAB
X = [1 2 3 4 5; 2 4 6 8 10];
covMatrix = cov(X);
disp(covMatrix);
AOutputs a 5x5 covariance matrix
BNo error, outputs a 2x2 covariance matrix
CError: Undefined function 'cov' for input arguments of type 'double'
DError: Inputs must be vectors or matrices with observations in rows
Attempts:
2 left
💡 Hint
Check how cov treats rows and columns as observations or variables.
🧠 Conceptual
advanced
2:00remaining
Interpret correlation matrix output
Given the MATLAB code below, what does the output matrix represent?
MATLAB
data = [1 2 3; 4 5 6; 7 8 9];
corrMatrix = corrcoef(data);
disp(corrMatrix);
ACorrelation coefficients between columns of data
BCorrelation coefficients between rows of data
CCovariance matrix of rows of data
DCovariance matrix of columns of data
Attempts:
2 left
💡 Hint
corrcoef by default computes correlation between columns.
❓ Predict Output
expert
2:00remaining
Covariance matrix with missing data
What is the output of this MATLAB code that calculates covariance with missing data (NaN)?
MATLAB
X = [1 NaN 3 4 5; 2 4 NaN 8 10];
covMatrix = cov(X', 'partialrows');
disp(covMatrix);
A[NaN NaN; NaN NaN]
B[2.5 5; 5 10]
C[2.9167 5.8333; 5.8333 14.3333]
DError: Invalid input argument 'partialrows'
Attempts:
2 left
💡 Hint
Check how cov handles NaN values with 'partialrows' option.

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