Solved – How to compute PCA scores from eigendecomposition of the covariance matrix

MATLABpca

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);

Best Answer

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.

Related Question