comparison toolboxes/MIRtoolbox1.3.2/somtoolbox/pcaproj.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function [P,V,me,l] = pcaproj(D,arg1,arg2)
2
3 %PCAPROJ Projects data vectors using Principal Component Analysis.
4 %
5 % [P,V,me,l] = pcaproj(D, odim)
6 % P = pcaproj(D, V, me)
7 %
8 % Input and output arguments ([]'s are optional)
9 % D (matrix) size dlen x dim, the data matrix
10 % (struct) data or map struct
11 % odim (scalar) how many principal vectors are used
12 %
13 % P (matrix) size dlen x odim, the projections
14 % V (matrix) size dim x odim, principal eigenvectors (unit length)
15 % me (vector) size 1 x dim, center point of D
16 % l (vector) size 1 x odim, the corresponding eigenvalues,
17 % relative to total sum of eigenvalues
18 %
19 % See also SAMMON, CCA.
20
21 % Contributed to SOM Toolbox 2.0, February 2nd, 2000 by Juha Vesanto
22 % Copyright (c) by Juha Vesanto
23 % http://www.cis.hut.fi/projects/somtoolbox/
24
25 % juuso 191297 070200
26
27 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
28
29 error(nargchk(2, 3, nargin)); % check the number of input arguments
30
31 % the data
32 if isstruct(D),
33 if strcmp(D.type,'som_map'), D=D.codebook; else D=D.data; end
34 end
35 [dlen dim] = size(D);
36
37 if nargin==2,
38
39 odim = arg1;
40
41 % autocorrelation matrix
42 A = zeros(dim);
43 me = zeros(1,dim);
44 for i=1:dim,
45 me(i) = mean(D(isfinite(D(:,i)),i));
46 D(:,i) = D(:,i) - me(i);
47 end
48 for i=1:dim,
49 for j=i:dim,
50 c = D(:,i).*D(:,j); c = c(isfinite(c));
51 A(i,j) = sum(c)/length(c); A(j,i) = A(i,j);
52 end
53 end
54
55 % eigenvectors, sort them according to eigenvalues, and normalize
56 [V,S] = eig(A);
57 eigval = diag(S);
58 [y,ind] = sort(abs(eigval));
59 eigval = eigval(flipud(ind));
60 V = V(:,flipud(ind));
61 for i=1:odim, V(:,i) = (V(:,i) / norm(V(:,i))); end
62
63 % take only odim first eigenvectors
64 V = V(:,1:odim);
65 l = abs(eigval)/sum(abs(eigval));
66 l = l(1:odim);
67
68 else % nargin==3,
69
70 V = arg1;
71 me = arg2;
72 odim = size(V,2);
73 D = D-me(ones(dlen,1),:);
74
75 end
76
77 % project the data using odim first eigenvectors
78 P = D*V;
79
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%