Principal Component Analysis (PCA) helps to simplify data by finding the main directions where the data changes the most. It reduces many numbers into fewer important ones.
Principal Component Analysis (PCA) in MATLAB
coeff = pca(X) [coeff, score, latent] = pca(X) % X is a data matrix where rows are observations and columns are variables.
pca function returns principal components in coeff.
score gives the data projected onto principal components.
X = [2.5 2.4; 0.5 0.7; 2.2 2.9; 1.9 2.2]; coeff = pca(X);
[coeff, score] = pca(X);
[coeff, score, latent] = pca(X);
This program runs PCA on a 10x2 data matrix. It shows the main directions (coeff), the data in the new system (score), and how much each direction explains the data (latent).
X = [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] = pca(X); fprintf('Principal Components (coeff):\n'); disp(coeff); fprintf('Data projected on principal components (score):\n'); disp(score); fprintf('Variance explained by each component (latent):\n'); disp(latent);
Make sure your data matrix X has rows as samples and columns as features.
PCA centers the data by default before calculating components.
Use latent to understand how much each component matters.
PCA finds main directions to simplify data.
Use pca function in MATLAB to get components and transformed data.
Check variance latent to see importance of each component.
Practice
What is the main purpose of Principal Component Analysis (PCA) in MATLAB?
Solution
Step 1: Understand PCA goal
PCA finds new axes (principal components) that capture most data variation.Step 2: Identify main purpose
It reduces data dimensions by focusing on important directions, simplifying analysis.Final Answer:
To reduce the number of variables by finding main directions of data -> Option AQuick Check:
PCA = Dimensionality reduction [OK]
- Thinking PCA sorts data
- Confusing PCA with summing data
- Assuming PCA generates random data
Which MATLAB command correctly performs PCA on a data matrix X?
Solution
Step 1: Recall MATLAB PCA syntax
The functionpcareturns coefficients, scores, and variances as outputs.Step 2: Match correct syntax
Only[coeff, score, latent] = pca(X);correctly calls PCA and stores outputs.Final Answer:
[coeff, score, latent] = pca(X); -> Option CQuick Check:
Correct PCA syntax =[coeff, score, latent] = pca(X);[OK]
- Using dot notation on matrix
- Passing output names as strings
- Assigning only one output without brackets
Given the MATLAB code below, what is the size of score if X is a 100x5 matrix?
[coeff, score, latent] = pca(X);
Solution
Step 1: Understand PCA output sizes
scorecontains transformed data points in principal component space, same rows asX.Step 2: Match dimensions
SinceXis 100x5,scoreis 100x5 (100 samples, 5 components).Final Answer:
100x5 -> Option BQuick Check:
score size = samples x components = 100x5 [OK]
- Confusing score size with coeff size
- Assuming score is square matrix
- Swapping rows and columns
Identify the error in this MATLAB PCA code snippet:
X = rand(50,3);
[coeff, score, latent] = pca(X);
disp(score(1,4));
Solution
Step 1: Check size of PCA outputs
InputXhas 3 columns, soscorehas 3 columns (components).Step 2: Identify indexing error
Accessingscore(1,4)tries to get 4th column which does not exist, causing an error.Final Answer:
Indexing score at column 4 causes an error -> Option AQuick Check:
score columns = 3, index 4 invalid [OK]
- Assuming more components than input columns
- Ignoring MATLAB indexing errors
- Thinking semicolon omission causes error here
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);
Solution
Step 1: Understand PCA dimension reduction parameter
MATLAB'spcaaccepts 'NumComponents' to limit output components.Step 2: Identify correct usage
[coeff, score] = pca(X, 'NumComponents', 2); uses correct parameter'NumComponents', 2to get 2 components directly.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.Final Answer:
[coeff, score] = pca(X, 'NumComponents', 2); -> Option DQuick Check:
Use 'NumComponents' to limit PCA output [OK]
- Using wrong parameter name 'Components'
- Using invalid slice indices on PCA score
- Assuming PCA auto-limits components without parameter
Ready to prove it?
Pass the test and this pattern moves unknown
