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