Why there is difference in MATLAB and RStudio outputs of PCA?
9 views (last 30 days)
Show older comments
Am running the followinf in-built example of RStudio in MATLAB:
I understand that RStudio by default scaled the data in addition to centre it before running the PCA. Therefore. am using zscore command of MATLAB before applying PCA. But I wonder why it is resulting into values smaller than RStudio results.
How can I fix this issue?
0 Comments
Answers (1)
Aditya
on 20 Aug 2025 at 6:48
Hi Maryam,
When performing Principal Component Analysis (PCA) in R using the prcomp() function with center=TRUE and scale.=TRUE, the data is both centered and scaled to unit variance before the analysis. MATLAB’s pca() function, on the other hand, centers the data by default but does not scale it unless explicitly instructed. If you want MATLAB’s PCA results to match those from R, you have two main options.
First, you can let MATLAB handle both centering and scaling by using the 'VariableWeights','variance' option in the pca() function. This tells MATLAB to scale each variable to unit variance, just as R does. Second, if you have already standardized your data using MATLAB’s zscore() function (which centers and scales the data), you should run pca() with the 'Centered', false option to prevent double centering.
However, you may still notice that MATLAB’s PCA scores (the principal component projections) are smaller in magnitude than those from R. This is because MATLAB’s scores are divided by sqrt(n-1), where n is the number of observations, while R’s scores are not. To directly compare MATLAB’s scores with those from R, simply multiply MATLAB’s scores by sqrt(n-1).
0 Comments
See Also
Categories
Find more on Dimensionality Reduction and Feature Extraction in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!