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.
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 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.
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.
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.
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.
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
pcafunction returns component directions, projected scores, and explained variance. - Standardize first when feature scales are not comparable.
- Use
score(:, 1:k)to keep only the firstkcomponents. - Choose
kby looking at explained and cumulative variance. - PCA is most useful for dimensionality reduction, visualization, and decorrelation.
Related reading
- Principal Component Analysis PCA on huge sparse dataset
- Principle of setting 'hash_bucket_size' parameter?
- Print top 10 users who post lots of questions and lots of answers of a Web Forum
- Print very long string in Pandas dataframe
- Print 2-D Array in clockwise expanding spiral from center
- Print a polynomial using minimum number of calls
- Printing Lists as Tabular Data
- Probability distribution in Python

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 courseTrack 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.