annotate toolboxes/FullBNT-1.0.7/KPMtools/pca_kpm.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function [pc_vec]=pca_kpm(features,N, method);
Daniel@0 2 % PCA_KPM Compute top N principal components using eigs or svd.
Daniel@0 3 % [pc_vec]=pca_kpm(features,N)
Daniel@0 4 %
Daniel@0 5 % features(:,i) is the i'th example - each COLUMN is an observation
Daniel@0 6 % pc_vec(:,j) is the j'th basis function onto which you should project the data
Daniel@0 7 % using pc_vec' * features
Daniel@0 8
Daniel@0 9 [d ncases] = size(features);
Daniel@0 10 fm=features-repmat(mean(features,2), 1, ncases);
Daniel@0 11
Daniel@0 12
Daniel@0 13 if method==1 % d*d < d*ncases
Daniel@0 14 fprintf('pca_kpm eigs\n');
Daniel@0 15 options.disp = 0;
Daniel@0 16 C = cov(fm'); % d x d matrix
Daniel@0 17 [pc_vec, evals] = eigs(C, N, 'LM', options);
Daniel@0 18 else
Daniel@0 19 % [U,D,V] = SVD(fm), U(:,i)=evec of fm fm', V(:,i) = evec of fm' fm
Daniel@0 20 fprintf('pca_kpm svds\n');
Daniel@0 21 [U,D,V] = svds(fm', N);
Daniel@0 22 pc_vec = V;
Daniel@0 23 end
Daniel@0 24
Daniel@0 25 if 0
Daniel@0 26 X = randn(5,3);
Daniel@0 27 X = X-repmat(mean(X),5,1);
Daniel@0 28 C = X'*X;
Daniel@0 29 C2=cov(X)
Daniel@0 30 [U,D,V]=svd(X);
Daniel@0 31 [V2,D2]=eig(X)
Daniel@0 32 end