Reputation: 2150
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
Upvotes: 0
Views: 5183
Reputation: 30579
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.)
Upvotes: 3