# How to compute PCA scores from eigendecomposition of the covariance matrix?

Given a data matrix $\mathbf X$ of $12 \times 7$ size with samples in rows and variables in columns, I have calculated centered data $\mathbf X_c$ by subtracting column means, and then computed covariance matrix as $\frac{1}{N-1} \mathbf X_c^\top \mathbf X_c$.

The dimensions of this covariance matrix are $7 \times 7$. After that I have calculated the eigenvalue decomposition, obtaining eigenvector matrix $\mathbf V$ with eigenvectors in columns.

Now I want to know about projections (i.e. principal component scores in PCA): is it $\mathbf{V}^\top \mathbf{X}_c$ or $\mathbf{X}_c \mathbf{V}$?

mean_matrix = X - repmat(mean(X),size(X,1),1);
covariance = (transpose(mean_matrix) * mean_matrix)/(12-1);

[V,D] = eig(covariance);
[e,i] = sort(diag(D), 'descend');
V_sorted = V(:,i);

• If you want the PCA, why not call [COEFF,SCORE] = princomp(X) (see here) directly? – Glen_b Jun 17 '14 at 22:45
• Also, why bother with the long road of standardizing the covariance matrix, when simply using the correlation matrix will get you there in one step? – Alexis Jun 18 '14 at 4:15
• it is just self understanding,that why i preferred to do it myself – dato datuashvili Jun 18 '14 at 5:10
• @Dato, in my previous answer to you there is clearly written: "Raw component scores ... are computed directly as X*V". All necessary formulas for PCA and factor analysis were given by me there. – ttnphns Jun 18 '14 at 5:38

The projection is given by $\mathbf{X}_c \mathbf{V}$; principal component scores are columns of this matrix.
Your first formula cannot be computed, as $\mathbf{V}^\top$ is of $7 \times 7$ size and $\mathbf{X}_c$ has $12$ rows; the dimensions do not match, and the matrix product is not defined. Instead, $\mathbf{V}^\top \mathbf{X}_c^\top$ would be another correct formula, but it is simply a transpose of the first equation: $\mathbf{V}^\top \mathbf{X}^\top_c = (\mathbf{X}_c\mathbf{V})^\top$. Here PC scores are in rows.