%fastpca.m
%Fast computation of PCA using Inner-product matrix 
load IMAGEDATA 
DATA=double(X)/255; 
%Time taken to compute the PCA basis in the direct method. 
tic 
M=mean(DATA'); 
s=0; 
for i=1:1:400 
    s=s+DATA(:,i)*DATA(:,i)'; 
end
s=s/400; 
s=s-M'*M; 
[E1,D1]=eigs(s,400); 
time1=toc; 
for i=1:1:400 
    E1(:,i)=E1(:,i)/(sqrt(sum(E1(:,i).^2))); 
end

%Using inner-product technique %Formulation of Inner-product matrix tic 
s=0; 
for i=1:1:400 
    for j=1:1:400 
        G(i,j)=DATA(:,i)'*DATA(:,j); 
    end
end
M=mean(G'); 
s=0; 
for i=1:1:400 
    s=s+G(:,i)*G(:,i)'; 
end
s=s/400; 
s=s-M'*M; 
[E,D]=eigs(inv(DATA'*DATA)*s,400);
E2=DATA*E;
for i=1:1:400 
    E2(:,i)=E2(:,i)/(sqrt(sum(E2(:,i).^2))); 
end
time2=toc; 
figure 
stem(sum((E1.*E2).^2))

