Bird
Raised Fist0
MATLABdata~5 mins

Correlation and covariance in MATLAB - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is covariance in statistics?
Covariance measures how two variables change together. If they increase or decrease together, covariance is positive; if one increases while the other decreases, covariance is negative.
Click to reveal answer
beginner
What does correlation measure?
Correlation measures the strength and direction of a linear relationship between two variables. It is normalized between -1 and 1.
Click to reveal answer
beginner
How do you calculate covariance in MATLAB?
Use the function cov(X) where X is a matrix or vectors of data. It returns the covariance matrix.
Click to reveal answer
beginner
How do you calculate correlation in MATLAB?
Use the function corrcoef(X) where X is a matrix or vectors. It returns the correlation coefficient matrix.
Click to reveal answer
intermediate
What is the difference between covariance and correlation?
Covariance shows how two variables vary together but depends on their units. Correlation standardizes this, showing strength and direction on a scale from -1 to 1.
Click to reveal answer
What does a correlation coefficient of 0 indicate?
ANo linear relationship between variables
BPerfect positive linear relationship
CPerfect negative linear relationship
DVariables are identical
Which MATLAB function returns the covariance matrix?
Acorrcoef
Bmean
Cstd
Dcov
If covariance between two variables is negative, what does it mean?
ABoth variables increase together
BVariables are independent
COne variable increases while the other decreases
DVariables have no relationship
What is the range of correlation coefficient values?
A0 to 1
B-1 to 1
C-∞ to ∞
D0 to 100
Which MATLAB function would you use to find the correlation between two data vectors X and Y?
Acorrcoef(X, Y)
Bcov(X, Y)
Cmean(X, Y)
Dstd(X, Y)
Explain the difference between covariance and correlation and how you calculate each in MATLAB.
Think about how covariance depends on units but correlation does not.
You got /4 concepts.
    Describe what a positive, negative, and zero covariance indicate about two variables.
    Consider how variables move together or apart.
    You got /3 concepts.

      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