-
Notifications
You must be signed in to change notification settings - Fork 4
/
pca.m
132 lines (89 loc) · 2.4 KB
/
pca.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
% Jervis Muindi
% Biometrics
% Dec 2011
function [ signals, PC, V ] = pca( data )
% Perform PCA.
% Data is the data matrix having dimensions in the columns and each image vector in the rows
% data - of size M by N
% PC - the principal components
% V - M by 1 of variances
% proj - the projected data
%
% Create the mean vector. This contains the means of each dimension
% of the data matrix (the cols)
[M,N] = size(data);
mn = mean(data,2);
%mn = mn'; %covnert to column vector
% subtract the mean of each dimension from the data matrix
disp('subtracting means..');
size(mn)
data = data - repmat(mn,1,N);
%calculate the convariance matrix.
size(data)
disp('computing conv');
covariance = 1 / (N-1) * data * data' ;
disp('finding eig vals');
%find the eigen vectors and eigen values
[PC, V] = eig(covariance);
%extract diagonal of matrix as vector which has the eigen values.
V = diag(V);
%sort the eigen values in decreasing order
[sorted, rindices] = sort(V, 'descend');
V = sorted;
disp(' e v');
%V
rindices = rindices(1:20, :);
%size(rindices)
%PC = PC(:, rindices);
disp('doing projections');
%project the original data set.
signals = PC' * data;
%{
disp('orig data');
size(data)
disp('proj data');
size(signals)
error('xxxxxxx');
%}
end
%{
function [ signals, PC, V ] = pca( data )
% Perform PCA.
% Data is the data matrix having dimensions in the columns and each image vector in the rows
% data - of size M by N
% PC - the principal components
% V - M by 1 of variances
% proj - the projected data
%
% Create the mean vector. This contains the means of each dimension
% of the data matrix (the cols)
[M,N] = size(data);
mn = mean(data,2);
% subtract the mean of each dimension from the data matrix
disp('subtracting means..');
data = data - repmat(mn,1,N);
%calculate the convariance matrix.
size(data)
disp('computing conv');
covariance = 1 / (N-1) * (data * data') ;
disp('finding eig vals');
%find the eigen vectors and eigen values
[PC, V] = eig(covariance);
%extract diagonal of matrix as vector which has the eigen values.
V = diag(V);
%sort the eigen values in decreasing order
[sorted, rindices] = sort(V, 'descend');
V = sorted;
%rindices = rindices(1:20, :);
%size(rindices)
PC = PC(:, rindices);
disp('doing projections');
%project the original data set.
signals = PC' * data;
disp('orig data');
size(data)
disp('proj data');
size(signals)
error('xxxxxxx');
end
%}