0

I'm just learning to use function pca in the statistics toolbox, when I tried the simple example below it returned an error but I can't find anything wrong?

pca_test_2=[1 1 1 1 1 ; 1.9 2.1 2 2 1.8]

I them multiple it by its transpose to get:

pca_test_2=pca_test_2*pca_test_2';

>> [coeff,score,latent]=pca(pca_test_2);
??? Undefined function or method 'pca' for input arguments of type 'double'.

I have looked but can't find anything that describes how to solve such an error?

The toolbox is definitely installed. When I use pcacov on a covariance matrix it works (for a different data set)?

Can someone please explain what I am doing wrong?

When I use the same data with svd with the same data set it also works?

[u s v]=svd(pca_test_2)

  u =

  -0.4537   -0.8912
  -0.8912    0.4537


  s =

  24.2493         0
        0    0.0107


  v =

  -0.4537   -0.8912
  -0.8912    0.4537

As advised I ran it again using princomp() see below;

However when comparing the results to those from scd I'm still a little confused: why are "u" of "coeff" not the same? (They are clearly similar though).

Why is latent not equal to the diagonal of s? Clearly they are completely different (I'm assuming its a scaling thing but given that the second eigenvalue on latent is zero its hard to see what scaling could have been used?)

[u s v]=svd(pca_test_3)

u =

  -0.4537   -0.8912
  -0.8912    0.4537

s =

  24.2493         0
        0    0.0107

v =

  -0.4537   -0.8912
  -0.8912    0.4537

[coeff,score,latent]=princomp(pca_test_2)

coeff =

   0.4525    0.8918
   0.8918   -0.4525

score =

  -5.3040         0
   5.3040         0

latent =

  56.2658
        0
2
  • use princomp instead of pca Commented Oct 29, 2013 at 17:55
  • Thanks it runs and produces results that are close but not the exactly right (I'm guessing princomp doesn't scale in the same way as svd but will have to check). I though princomp was a legacy function and pca was the newer better version. Why is pca not working here? More generally how can I know which method should I choose in any given situation: svd, pca, princomp and why? Commented Oct 29, 2013 at 18:04

1 Answer 1

3

You can use either pca or princomp, since princomp just calls pca. See here.

SVD and PCA are related, but not the same. PCA applied to a data set is essentially the SVD of the mean-centered data set or the eigen vectors of the covariance matrix. And as noted here, the square roots of the eigen values of the covariance matrix are the singular values. The MATLAB PCA functions use svd in certain cases and eig in others.

The source of the "Undefined function or method 'pca'..." error is probably because you either have a variable called pca or another pca.m somewhere on your path. Try which -all pca to see if there are multiple functions, and whos pca to see if there is another variable. A more general test is exist('pca') which will give you a number coding the type of object found (file, variable, etc.)

Sign up to request clarification or add additional context in comments.

1 Comment

Look in pca.m to see how it prepares the data for use in svd. It will run x = bsxfun(@minus,x,mu); to remove the mean of each dimension. Then it will run the SVD. pca.m also scales the results correctly. It also enforces a sign convention on the coefficients... just have a look at the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.