Mercurial > hg > camir-aes2014
annotate toolboxes/FullBNT-1.0.7/KPMtools/pca_kpm.m @ 0:e9a9cd732c1e tip
first hg version after svn
author | wolffd |
---|---|
date | Tue, 10 Feb 2015 15:05:51 +0000 |
parents | |
children |
rev | line source |
---|---|
wolffd@0 | 1 function [pc_vec]=pca_kpm(features,N, method); |
wolffd@0 | 2 % PCA_KPM Compute top N principal components using eigs or svd. |
wolffd@0 | 3 % [pc_vec]=pca_kpm(features,N) |
wolffd@0 | 4 % |
wolffd@0 | 5 % features(:,i) is the i'th example - each COLUMN is an observation |
wolffd@0 | 6 % pc_vec(:,j) is the j'th basis function onto which you should project the data |
wolffd@0 | 7 % using pc_vec' * features |
wolffd@0 | 8 |
wolffd@0 | 9 [d ncases] = size(features); |
wolffd@0 | 10 fm=features-repmat(mean(features,2), 1, ncases); |
wolffd@0 | 11 |
wolffd@0 | 12 |
wolffd@0 | 13 if method==1 % d*d < d*ncases |
wolffd@0 | 14 fprintf('pca_kpm eigs\n'); |
wolffd@0 | 15 options.disp = 0; |
wolffd@0 | 16 C = cov(fm'); % d x d matrix |
wolffd@0 | 17 [pc_vec, evals] = eigs(C, N, 'LM', options); |
wolffd@0 | 18 else |
wolffd@0 | 19 % [U,D,V] = SVD(fm), U(:,i)=evec of fm fm', V(:,i) = evec of fm' fm |
wolffd@0 | 20 fprintf('pca_kpm svds\n'); |
wolffd@0 | 21 [U,D,V] = svds(fm', N); |
wolffd@0 | 22 pc_vec = V; |
wolffd@0 | 23 end |
wolffd@0 | 24 |
wolffd@0 | 25 if 0 |
wolffd@0 | 26 X = randn(5,3); |
wolffd@0 | 27 X = X-repmat(mean(X),5,1); |
wolffd@0 | 28 C = X'*X; |
wolffd@0 | 29 C2=cov(X) |
wolffd@0 | 30 [U,D,V]=svd(X); |
wolffd@0 | 31 [V2,D2]=eig(X) |
wolffd@0 | 32 end |