Daniel@0: function [pc_vec]=pca_kpm(features,N, method); Daniel@0: % PCA_KPM Compute top N principal components using eigs or svd. Daniel@0: % [pc_vec]=pca_kpm(features,N) Daniel@0: % Daniel@0: % features(:,i) is the i'th example - each COLUMN is an observation Daniel@0: % pc_vec(:,j) is the j'th basis function onto which you should project the data Daniel@0: % using pc_vec' * features Daniel@0: Daniel@0: [d ncases] = size(features); Daniel@0: fm=features-repmat(mean(features,2), 1, ncases); Daniel@0: Daniel@0: Daniel@0: if method==1 % d*d < d*ncases Daniel@0: fprintf('pca_kpm eigs\n'); Daniel@0: options.disp = 0; Daniel@0: C = cov(fm'); % d x d matrix Daniel@0: [pc_vec, evals] = eigs(C, N, 'LM', options); Daniel@0: else Daniel@0: % [U,D,V] = SVD(fm), U(:,i)=evec of fm fm', V(:,i) = evec of fm' fm Daniel@0: fprintf('pca_kpm svds\n'); Daniel@0: [U,D,V] = svds(fm', N); Daniel@0: pc_vec = V; Daniel@0: end Daniel@0: Daniel@0: if 0 Daniel@0: X = randn(5,3); Daniel@0: X = X-repmat(mean(X),5,1); Daniel@0: C = X'*X; Daniel@0: C2=cov(X) Daniel@0: [U,D,V]=svd(X); Daniel@0: [V2,D2]=eig(X) Daniel@0: end