-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpca.m
executable file
·43 lines (31 loc) · 1.08 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
%Input:
% data - a mxn matrix, where n is the number of cases,
% m is the dimension of each case
% new_dimensions - the number of dimensions of the new data.
% must be <= m
%
%Output:
% projected_data - a cxn matrix, where c = new_dimensions
%
function [projected_data] = pca(data, new_dimensions)
C = zeros(size(data, 1), size(data, 1));
N = size(data, 2);
double_data = double(data);
% find the mean vector
theMean = mean(data, 2);
% find the matrix C that we will find eigenvectors from
for i = 1:N
sub = double_data(:, i) - theMean;
C = C + ((1/N) * sub) * transpose(sub);
end
% find eigenvectors and eigenvalues
[eigVec, eigVal] = eig(C);
% store indices of sorted eigenvalues
[a, indices] = sort(diag(eigVal), 'descend');
% sort eigenvectors according to indices
eigVec = eigVec(:, indices);
% 256 x m matrix
U = eigVec(:, 1:new_dimensions);
% find projected data
projected_data = transpose(U) * double_data;
end