matlabpcaorthogonalprincipal-components

Why 'pca' in Matlab doesn't give orthogonal principal components?


Why using pca in Matlab, I cannot get the orthogonal principal component matrix

For example:

A=[3,1,-1;2,4,0;4,-2,-5;11,22,20];

A =

    3     1    -1
    2     4     0
    4    -2    -5
   11    22    20



>> W=pca(A)

W =

    0.2367    0.9481   -0.2125
    0.6731   -0.3177   -0.6678
    0.7006   -0.0150    0.7134


>> PCA=A*W

PCA =

    0.6826    2.5415   -2.0186
    3.1659    0.6252   -3.0962
   -3.9026    4.5028   -3.0812
   31.4249    3.1383   -2.7616

Here, every column is a principle component. So,

>> PCA(:,1)'*PCA(:,2)

ans =

   84.7625

But the principle component matrix hasn't mutually orthogonal components.

I checked some materials, it said they are not only uncorrelated, but strictly orthogonal. But I can't get the desired result. Can somebody tell me where I went wrong?

Thanks!


Solution

  • You are getting confused between the representation of A in the PCA feature space and the principal components. W are the principal components, and they will indeed be orthogonal.

    Check that W(:,1).'*W(:,2) = 5.2040e-17, W(:,1).'*W(:,3) = -1.1102e-16 -- indeed orthogonal

    What you are trying to do is to transform the data (i.e. A) in the PCA feature space. You should mean center the data first and then multiply by the principal components as follows.

    % A_PCA = (A-repmat(mean(A),4,1))*W
    % A more efficient alternative to the above command
    A_PCA = bsxfun(@minus,A,mean(A))*W
    % verify that it is correct by comparing it with `score` - i.e. the PCA representation 
    % of A given by MATLAB.
    [W, score] = pca(A); % mean centering will occur inside pca
    (score-(A-repmat(mean(A),4,1))*W) % elements are of the order of 1e-14, hence equal.