Principal Component Analysis
MATLAB
Data Analysis
PCA Algorithm
Dimensionality Reduction

Principal Component Analysis in MATLAB

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Principal Component Analysis, or PCA, reduces a dataset into a smaller number of orthogonal directions that capture as much variance as possible. In MATLAB, the built-in pca function handles most of the heavy lifting, so the real work is understanding what to standardize, how to interpret the outputs, and how many components to keep. PCA is useful for dimensionality reduction, visualization, denoising, and feature preparation.

What PCA Produces

PCA transforms the original variables into new variables called principal components.

The first component explains the largest possible variance, the second explains the next largest variance while remaining orthogonal to the first, and so on.

In MATLAB, the main outputs of pca are usually:

  • 'coeff: component directions'
  • 'score: projected data in the new basis'
  • 'latent: variance explained by each component'
  • 'explained: percentage of variance explained'

A Basic MATLAB Example

matlab
1X = [2 4 1;
2     3 6 2;
3     4 8 3;
4     5 10 4;
5     6 12 5];
6
7[coeff, score, latent, tsquared, explained, mu] = pca(X);
8
9disp(coeff)
10disp(score)
11disp(explained)

MATLAB centers the variables by default before computing the principal components. That is usually what you want.

Standardization Matters

If the variables are on very different scales, standardize them before PCA. Otherwise, a large-scale variable can dominate the principal components just because of its units.

matlab
X = zscore(X);
[coeff, score, latent, tsquared, explained] = pca(X);

Use raw centered data when the original scale differences are meaningful. Use standardized data when the variables are measured in very different units and you want comparable influence.

Reducing to Fewer Components

Once you run PCA, you can keep only the first k components.

matlab
k = 2;
X_reduced = score(:, 1:k);
disp(X_reduced)

This is the reduced representation of the original dataset. If your original data had many correlated columns, the reduced matrix can retain most of the useful structure with fewer dimensions.

Choosing How Many Components to Keep

A common decision rule is to look at cumulative explained variance.

matlab
cumulative = cumsum(explained);
disp(cumulative)

If the first two or three components explain most of the variance, keeping only those may be reasonable. There is no universal threshold, but many workflows look for something like 90 percent or 95 percent cumulative variance.

This is a modeling decision, not a MATLAB-specific rule.

Visualizing PCA Results

PCA is often used for two-dimensional visualization.

matlab
1scatter(score(:,1), score(:,2))
2xlabel('PC1')
3ylabel('PC2')
4title('PCA Projection')

This is useful for seeing clusters, outliers, or rough class separation after dimensionality reduction.

Interpretation Requires Care

The loadings in coeff tell you how each original variable contributes to each component.

Large positive or negative values in the same component indicate which original variables are driving that direction of variation. However, principal components are mathematical variance directions, not automatically interpretable business concepts.

So PCA is often best treated as a compression and visualization tool first, and an interpretation tool second.

Common Pitfalls

  • Running PCA on variables with very different scales without standardizing when scale should not dominate.
  • Keeping too many or too few components without checking explained variance.
  • Treating principal components as automatically meaningful real-world factors.
  • Forgetting that MATLAB centers the data by default and then manually centering it twice without a plan.
  • Using PCA for supervised prediction without checking whether the components actually preserve the signal relevant to the target.

Summary

  • MATLAB's pca function returns component directions, projected scores, and explained variance.
  • Standardize first when feature scales are not comparable.
  • Use score(:, 1:k) to keep only the first k components.
  • Choose k by looking at explained and cumulative variance.
  • PCA is most useful for dimensionality reduction, visualization, and decorrelation.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.