# PCA output of Matlab's pca() function doesn't match manual calculation

I try to calculate the PCA in my matrix and I use two ways for this:

1. PCA function

[coeff, score, eigenvalues] = pca(M);

2. And for compare and understand the PCA calculus, I try to calculate step by step the PCA without the matlab function pca.

%// first I "z-scored" my matrix
X = zscore(M);
%// second I calculate the covariance matrix
%// this matrix is equals to the correlation matrix
V = cov(X);
%// Third, I calculate the eigenvalues(E) and eigenvectors(U)
[U,E] = eig(V);


The pca function's eigenvalues are not equal to E and I think the columns of U are principal components and rows of U are variables and it's not equal to coeff.

So, I think that I don't understand how calculate the PCA of a matrix?

• I don't use MATLAB often, but its pca function might not do a z-score first. The preliminary standardization is helpful for statistical applications, but other applications of PCA don't require any transformation so the standard MATLAB call might not include it. – EdM Jul 29 '15 at 23:38
• I replace M by X in the pca function's and i don't found a equal result. My "manual" calculus have sense or not? – sushi Jul 30 '15 at 1:54
• mathworks.com/help/stats/pca.html says pca(data) centers the data. But you did z-standardization X = zscore(M). That is, center-then-scale operation. – ttnphns Jul 30 '15 at 10:39
• Sushi, please edit your question to reflect changes you did after @EdM's comment (+1). – amoeba Jul 30 '15 at 14:18
• It would help if you could show a simple example that illustrates the problem, like a 3 x 3 matrix. – EdM Jul 30 '15 at 14:38

The problem is the zscore function. If I do a "manual" z-scoring in my matrix I find the same result as with pca:

M = [10,5,14;12,5,45;123,58,42];
%// "manual" zscore
stdr = std(M);
X = M./repmat(stdr,size(M,1),1);
%// "manual" PCA
V = cov(X);
[U,E] = eig(V);
%// with pca function
[coeff,score,eigenvalue] = pca(X);


E equals eigenvalue and coeff equals U so I'm ok, I think I understand how calculate a PCA.

• If you run cov(zscore(M)) you will get exactly the same output as with cov(X) (I checked), so I don't think that there is any problem with the zscore function. Your manual zscoring produces identical output. – amoeba Jul 30 '15 at 20:59
• The coeff result is sorted, maybe i don't pay attention at this before. – sushi Jul 30 '15 at 21:28