-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.m
46 lines (36 loc) · 1.06 KB
/
main.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
%Reading txt file
eachData = importdata('pca-data.txt');
%Principal Component Analysis
%Each mean calcualting
meanX=meanCal(eachData(:,1))
meanY=meanCal(eachData(:,2))
meanZ=meanCal(eachData(:,3))
totalMean=[meanX, meanY, meanZ];
covariMatrix=(eachData-totalMean)' * eachData-totalMean;
covariMatrix=covariMatrix/size(eachData,1);
[eigVec,eigValue] = eig(covariMatrix);
% first two principal components
firstEigenVector=eigVec(:,1)
firstEigenValue=eigValue(1,1)
secondEigenVector=eigVec(:,2)
secondEigenValue=eigValue(2,2)
% figure
% scatter3(eachData(:,1),eachData(:,2),eachData(:,3),'ro');
% hold on
% scatter3(meanX,meanY,meanZ,10000,'y+');
% scatter3(meanX,meanY,meanZ,10000,'b.');
% %plotv(eigVec + totalMean' ,'-')
% %plotv(eigVec ,'-')
% hold off
%
%
% % projection
% figure
% projectedData=eachData * eigVec;
% scatter(projectedData(:,1),projectedData(:,2));
% figure
% scatter(projectedData(:,1),projectedData(:,3));
% figure
% scatter(projectedData(:,2),projectedData(:,3));
% %scatter3(projectedData(:,1),projectedData(:,2),projectedData(:,3));
% hold off