Which one should I use to downsize with PCA in MATLAB, pcacov, or eigs?
I am trying to reduce the size of the trainable set from 1296 * 70,000 to 128 * 70,000. I wrote below code:
A=DicH; [M N]=size(A); mu=mean(A,2);%mean of columns Phi=zeros(M,N); C=zeros(M,M); for j=1:N Phi(:,j)=A(:,j)-mu; c=Phi(:,j)*(Phi(:,j))'; C=C+c; end C=C/N;%Covariance Dictionary [V,landa] = eigs(C,128);%Eigen Vectors & Eigen Values E=V'*Phi;%Reduced Dic %*******************Using Pcacov***************** %S=zeros(M,1); %[U,landa] = pcacov(C);%Eigen Vectors & Eigen Values % for k=1:128; % S=V(:,k)+S; % U(:,k)=S; % end %E=U'*Phi;%Reduced Dic
I am getting two different answers! Which one should I use "aegi" or "pkakov"?
source to share
You have to use the built-in functions in Matlab and use the function pca
directly, or even a function cov
if you want to compare eigs
with pcaconv
.
Now, to answer your question, both return the same eigenvectors, but not in the same order. See next example:
>> load hald >> covx = cov(ingredients); >> [COEFF,latent] = pcacov(covx) COEFF = -0.0678 -0.6460 0.5673 0.5062 -0.6785 -0.0200 -0.5440 0.4933 0.0290 0.7553 0.4036 0.5156 0.7309 -0.1085 -0.4684 0.4844 latent = 517.7969 67.4964 12.4054 0.2372 >> [V, D] = eigs(covx) V = 0.5062 0.5673 0.6460 -0.0678 0.4933 -0.5440 0.0200 -0.6785 0.5156 0.4036 -0.7553 0.0290 0.4844 -0.4684 0.1085 0.7309 D = 0.2372 0 0 0 0 12.4054 0 0 0 0 67.4964 0 0 0 0 517.7969 >>
In your code, you are overwriting the result pcavconv
in the commented-out section with the conversion of the result eigs
, so it is not clear what you are comparing at this point. When using, pcacov
you just need to extract the first 128 columns U
.
source to share