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