Bird
Raised Fist0
MATLABdata~20 mins

Principal Component Analysis (PCA) in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PCA Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this PCA variance explained code?
Given the following MATLAB code that performs PCA on a dataset, what is the output of the variable explainedVariance?
MATLAB
data = [2.5 2.4; 0.5 0.7; 2.2 2.9; 1.9 2.2; 3.1 3.0; 2.3 2.7; 2 1.6; 1 1.1; 1.5 1.6; 1.1 0.9];
[coeff, score, latent, tsquared, explained] = pca(data);
explainedVariance = round(explained, 1);
A[ 95.6 4.4 ]
B[ 99.9 0.1 ]
C[ 80.0 20.0 ]
D[ 50.0 50.0 ]
Attempts:
2 left
💡 Hint
Think about how PCA finds directions with most variance and how explained variance is calculated.
🧠 Conceptual
intermediate
1:30remaining
Which statement best describes the role of eigenvectors in PCA?
In Principal Component Analysis, what do the eigenvectors of the covariance matrix represent?
AThey represent the noise components to be removed.
BThey represent the mean values of each variable.
CThey represent the directions of maximum variance in the data.
DThey represent the original data points in reduced form.
Attempts:
2 left
💡 Hint
Think about what PCA tries to find in the data.
🔧 Debug
advanced
2:00remaining
Why does this PCA code produce an error?
Consider this MATLAB code snippet for PCA:
data = rand(10,3);
[coeff, score, latent] = pca(data);
reducedData = score(:, 4);
Why does this code produce an error?
ABecause 'score' has only 3 columns, accessing the 4th column causes an index error.
BBecause 'pca' requires the data to be normalized first.
CBecause 'latent' is not assigned correctly in the output.
DBecause 'coeff' must be transposed before use.
Attempts:
2 left
💡 Hint
Check the size of the 'score' matrix relative to the input data dimensions.
📝 Syntax
advanced
1:30remaining
Which option correctly computes the covariance matrix for PCA?
In MATLAB, which code snippet correctly computes the covariance matrix of the data matrix X for PCA?
AcovMatrix = cov(X);
BcovMatrix = cov(X, 1);
CcovMatrix = X' * X;
DcovMatrix = cov(X, 0);
Attempts:
2 left
💡 Hint
Remember that MATLAB's cov function uses normalization by (N-1) by default; the second argument controls normalization.
🚀 Application
expert
2:00remaining
How many principal components are needed to explain at least 90% variance?
Given the explained variance ratios from PCA on a dataset as follows:
explained = [40.5, 25.0, 15.0, 10.0, 5.0, 4.5];
How many principal components are needed to explain at least 90% of the total variance?
A3
B4
C5
D6
Attempts:
2 left
💡 Hint
Add the explained variances cumulatively until the sum reaches or exceeds 90%.

Practice

(1/5)
1.

What is the main purpose of Principal Component Analysis (PCA) in MATLAB?

easy
A. To reduce the number of variables by finding main directions of data
B. To sort data in ascending order
C. To calculate the sum of all data points
D. To generate random numbers for data simulation

Solution

  1. Step 1: Understand PCA goal

    PCA finds new axes (principal components) that capture most data variation.
  2. Step 2: Identify main purpose

    It reduces data dimensions by focusing on important directions, simplifying analysis.
  3. Final Answer:

    To reduce the number of variables by finding main directions of data -> Option A
  4. Quick Check:

    PCA = Dimensionality reduction [OK]
Hint: PCA simplifies data by finding main directions [OK]
Common Mistakes:
  • Thinking PCA sorts data
  • Confusing PCA with summing data
  • Assuming PCA generates random data
2.

Which MATLAB command correctly performs PCA on a data matrix X?

easy
A. score = pca(X, 'score');
B. pca = X.coeff();
C. [coeff, score, latent] = pca(X);
D. coeff = pca(X, 'coeff');

Solution

  1. Step 1: Recall MATLAB PCA syntax

    The function pca returns coefficients, scores, and variances as outputs.
  2. Step 2: Match correct syntax

    Only [coeff, score, latent] = pca(X); correctly calls PCA and stores outputs.
  3. Final Answer:

    [coeff, score, latent] = pca(X); -> Option C
  4. Quick Check:

    Correct PCA syntax = [coeff, score, latent] = pca(X); [OK]
Hint: Use brackets to capture multiple PCA outputs [OK]
Common Mistakes:
  • Using dot notation on matrix
  • Passing output names as strings
  • Assigning only one output without brackets
3.

Given the MATLAB code below, what is the size of score if X is a 100x5 matrix?

 [coeff, score, latent] = pca(X); 
medium
A. 5x5
B. 100x5
C. 100x100
D. 5x100

Solution

  1. Step 1: Understand PCA output sizes

    score contains transformed data points in principal component space, same rows as X.
  2. Step 2: Match dimensions

    Since X is 100x5, score is 100x5 (100 samples, 5 components).
  3. Final Answer:

    100x5 -> Option B
  4. Quick Check:

    score size = samples x components = 100x5 [OK]
Hint: Score matches rows of X and number of components [OK]
Common Mistakes:
  • Confusing score size with coeff size
  • Assuming score is square matrix
  • Swapping rows and columns
4.

Identify the error in this MATLAB PCA code snippet:

 X = rand(50,3);
[coeff, score, latent] = pca(X);
disp(score(1,4));
medium
A. Indexing score at column 4 causes an error
B. Missing semicolon after rand function
C. Incorrect function name, should be pca_analysis
D. Matrix X size is invalid for PCA

Solution

  1. Step 1: Check size of PCA outputs

    Input X has 3 columns, so score has 3 columns (components).
  2. Step 2: Identify indexing error

    Accessing score(1,4) tries to get 4th column which does not exist, causing an error.
  3. Final Answer:

    Indexing score at column 4 causes an error -> Option A
  4. Quick Check:

    score columns = 3, index 4 invalid [OK]
Hint: Check matrix size before indexing columns [OK]
Common Mistakes:
  • Assuming more components than input columns
  • Ignoring MATLAB indexing errors
  • Thinking semicolon omission causes error here
5.

You want to reduce a dataset X (200x10) to 2 principal components using MATLAB PCA. Which code correctly achieves this?

 % Options:
A) [coeff, score] = pca(X, 'NumComponents', 2);
B) [coeff, score] = pca(X, 'NumComponents', 2); score = score(:,3:4);
C) [coeff, score] = pca(X); score = score(:,11:12);
D) [coeff, score] = pca(X, 'Components', 2);
hard
A. [coeff, score] = pca(X, 'Components', 2);
B. [coeff, score] = pca(X, 'NumComponents', 2); score = score(:,3:4);
C. [coeff, score] = pca(X); score = score(:,11:12);
D. [coeff, score] = pca(X, 'NumComponents', 2);

Solution

  1. Step 1: Understand PCA dimension reduction parameter

    MATLAB's pca accepts 'NumComponents' to limit output components.
  2. Step 2: Identify correct usage

    [coeff, score] = pca(X, 'NumComponents', 2); uses correct parameter 'NumComponents', 2 to get 2 components directly.
  3. Step 3: Check other options

    B: [coeff, score] = pca(X, 'NumComponents', 2); score = score(:,3:4); tries to slice non-existent columns 3-4; C: [coeff, score] = pca(X); score = score(:,11:12); tries to slice non-existent columns 11-12; D: [coeff, score] = pca(X, 'Components', 2); uses invalid parameter.
  4. Final Answer:

    [coeff, score] = pca(X, 'NumComponents', 2); -> Option D
  5. Quick Check:

    Use 'NumComponents' to limit PCA output [OK]
Hint: Use 'NumComponents' to get desired PCA components [OK]
Common Mistakes:
  • Using wrong parameter name 'Components'
  • Using invalid slice indices on PCA score
  • Assuming PCA auto-limits components without parameter
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