-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperformpca.m
49 lines (40 loc) · 1.36 KB
/
performpca.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
function new_data = performpca(X,T, flag_norm, is2D)
% PERFORMPCA Perform pca analysis
% PERFORMPCA(X, T, filename, dir) takes the matrix of data
% X and the class labels T and performs a principal component analysis
% based on sprtool. A figure with the scree plot is saved, according to
% the given filename. PERFORMPCA saves the png files of scree
% plot in directory dir.
%
% OUTPUT:
% pca_data.X: matrix of the projection
% pca_data.y: matrix of class labels
% pca_data.filename: name of data
% pca_data.Kaiser: components to keep according to Kaiser criterion
% pca_data.eigval = eigenvalues
% Normalize Data: FLAG 1/0
if flag_norm
X = scalestd(X);
end
in_data = structdata(X,T);
model = pca(in_data.X,3);
out_data = linproj(in_data, model);
% pca_data.eigval = model.eigval;
% pca_data.filename = filename;
% pca_data.X = pca_data.X';
% pca_data.y = pca_data.y';
% ppatterns(out_data);
new_data.X = out_data.X';
new_data.Y = out_data.y';
figure;
if is2D
gscatter(new_data.X(:,1), new_data.X(:,2) ,new_data.Y, 'rb','ox');
else
h = gscatter(new_data.X(:,1),new_data.X(:,2), new_data.Y, 'rb','ox');
z = new_data.X(:,3);
gu = unique(new_data.Y);
for k = 1:numel(gu)
idx = (new_data.Y == gu(k));
set(h(k), 'ZData', z(idx));
end
end