You are on page 1of 6

Ram Sharan Timilsina(065/BEL/327)

Principal Component Analysis Matlab code

clear();
fprintf('Enter the no of sample\n');
n=input('Number of row=');
fprintf('Enter the number of varaible\n');
m=input('Number of column=');
fprintf('enter the data matrix');
a=input('[a]=')
%calculation of mean matrix
t=0;
for j=1:1:m
t=t+1;
sum(t)=0;
for i=1:1:n
sum(t)=sum(t)+a(i,j);
end
mean(t)=sum(t)/n;
end
%calculation of modified data
for j=1:1:m
for i=1:1:n
am(i,j)=a(i,j)-mean(j);
end
end
display('modified data is')
display(am)
%calculation of covariance matrix,eigenvalues of it and corresponding eigen
%vectors
covm=cov(a);
display(covm);
[eigenvector,eigenvalues]=eig(covm);
display(eigenvector);
display(eigenvalues);
%arranging the matrix of eigenvector in decreasing value of eigen valuek=
k=0;
for i=1:1:m
for j=1:1:m
if(i==j)
k=k+1;
d(k)=eigenvalues(i,j);
end
end
end
display(d);
dsorted=sort(d,'descend');

Ram Sharan Timilsina(065/BEL/327)

%finding the position of largest eigen vector


z=0;
for k=1:1:m
for l=1:1:m
if(dsorted(k)==d(l))
z=z+1;
position(z)=l;
end
end
end
display(position);
%swaping the eigenvector in their position given by position vector
%finding the number of Eigen value used, count display the number of value
used to extract the data
sum=0;
for i=1:1:m
sum=sum+dsorted(i);
end
cnt=1;
cntsum=0;
o=1;
for i=1:1:m
cntsum=cntsum+dsorted(i);
f=cntsum/sum;
if(f<0.9)
cnt=cnt+1;
end
end
display(cnt);
%obtaining the feature vector fev
for l=1:1:cnt
for i=1:1:m
a=position(l);
fev(i,l)=eigenvector(i,a);
end
end

display(fev);
%calculation of new data
newdata=transpose(fev)*transpose(am);
display('final data is =')
display(newdata);

Ram Sharan Timilsina(065/BEL/327)

Execution of code
1. For three variable
Enter the no of sample,=Number of row=15
Enter the number of variable= Number of column=3
Enter the data matrix[a]= [68 98 19; 67 96 15; 67 80 27 ; 66 69 41 ; 66 74 29 ; 66 56 40 ; 66 82 103 ;
66 84 198 ; 65 100 11; 64 71 64 ; 63 51 40;63 85 62; 60 83 12;60 61 29; 59 77 18]
a=
68 98 19
67 96 15
67 80 27
66 69 41
66 74 29
66 56 40
66 82 103
66 84 198
65 100 11
64 71 64
63 51 40
63 85 62
60 83 12
60 61 29
59 77 18
modified data is:
[am] = 3.6000 20.2000 -28.2000
2.6000 18.2000 -32.2000
2.6000 2.2000 -20.2000
1.6000 -8.8000 -6.2000
1.6000 -3.8000 -18.2000
1.6000 -21.8000 -7.2000
1.6000 4.2000 55.8000
1.6000 6.2000 150.8000
0.6000 22.2000 -36.2000
-0.4000 -6.8000 16.8000
-1.4000 -26.8000 -7.2000
-1.4000 7.2000 14.8000
-4.4000 5.2000 -35.2000
-4.4000 -16.8000 -18.2000
-5.4000 -0.8000 -29.2000

Ram Sharan Timilsina(065/BEL/327)

covm = 1.0e+003 *
0.0080 0.0133 0.0283
0.0133 0.2133 -0.0159
0.0283 -0.0159 2.3430
eigenvector =
-0.9978 0.0653 0.0121
0.0652 0.9978 -0.0074
0.0125 0.0066 0.9999
eigenvalues = 1.0e+003 *
0.0067
0
0
0
0.2141
0
0
0
2.3435
d = 1.0e+003 (0.0067 0.2141 2.3435)
position = 3 2 1
cnt = 1
Feature vector, [fv]T = [ 0.0121 -0.0074 0.9999]
Final data is = -28.302, -32.2998, -20.1829, -6.1151, -18.1508, -7.019, 55.7827, 150.7585, -36.3530,
16.8437, -7.0183 14.7285, -35.2879, -18.1272, -29.2563

2. For five variable


Enter the no of sample = Number of row=15
Enter the number of variable= Number of column=5
enter the data matrix[a]=[68 63 98 75 19;67 62 96 68 15 ;67 66 80 64 27 ;66 64 69 64 41 ;66 64
74 63 29; 66 66 56 78 40 ;66 50 82 76 103 ; 66 42 84 66 198; 65 62 100 78 11; 64 55 71 74 64; 63
65 51 63 40; 63 41 85 78 62; 60 59 83 66 12 ;60 62 61 61 29; 59 59 77 52 18]

a = 68
67
67
66
66
66
66
66
65
64
63
63
60
60
59

63 98 75 19
62
66
64
64
66
50
42
62
55
65
41
59
62
59

96
80
69
74
56
82
84
100
71
51
85
83
61
77

68
64
64
63
78
76
66
78
74
63
78
66
61
52

15
27
41
29
40
103
198
11
64
40
62
12
29
18

Ram Sharan Timilsina(065/BEL/327)

Modified data is ,
am =
3.6000
2.6000
2.6000
1.6000
1.6000
1.6000
1.6000
1.6000
0.6000
-0.4000
-1.4000
-1.4000
-4.4000
-4.4000
-5.4000

4.3333 20.2000 6.6000 -28.2000


3.3333 18.2000 -0.4000 -32.2000
7.3333 2.2000 -4.4000 -20.2000
5.3333 -8.8000 -4.4000 -6.2000
5.3333 -3.8000 -5.4000 -18.2000
7.3333 -21.8000 9.6000 -7.2000
-8.6667 4.2000 7.6000 55.8000
-16.6667 6.2000 -2.4000 150.8000
3.3333 22.2000 9.6000 -36.2000
-3.6667 -6.8000 5.6000 16.8000
6.3333 -26.8000 -5.4000 -7.2000
-17.6667 7.2000 9.6000 14.8000
0.3333 5.2000 -2.4000 -35.2000
3.3333 -16.8000 -7.4000 -18.2000
0.3333 -0.8000 -16.4000 -29.2000

covm = 1.0e+003 *
0.0080 0.0024
0.0024 0.0668
0.0133 -0.0325
0.0106 -0.0169
0.0283 -0.2949
eigenvector =
-0.9053 0.3971
0.3728 0.9010
0.0892 0.1315
0.1742 0.0331
0.0550 0.1101
eigenvalues = 1.0e+003 *
0.0019
0
0
0.0264
0
0
0
0
0
0
0
0

0.0133
-0.0325
0.2133
0.0355
-0.0159

0.0106
-0.0169
0.0355
0.0601
0.0471

0.0283
-0.2949
-0.0159
0.0471
2.3430

0.1360
-0.0569
-0.2374
0.9597
-0.0304

-0.0635
0.1731
-0.9583
-0.2171
0.0226

0.0118
-0.1264
-0.0050
0.0210
0.9917

0
0
0
0
0
0
0.0523 0
0
0.2285
0
0
2.3820

Ram Sharan Timilsina(065/BEL/327)

d = 1.0e+003 ( 0.0019 0.0264 0.0523 0.2285 2.3820)

position =[ 5 4
cnt = 2

fev =

1]

0.0118 -0.0635
-0.1264 0.1731

-0.0050
0.9583
0.0210 -0.2171
0.9917 0.0226
Final data is =[Y1,Y2]=[ -28.4320 -32.4213 -21.0316 -6.8526 -18.7985 -7.7384 56.5887 151.5888
-36.2210 17.2703 - 7.9378 17.0595 -35.0770 -18.5939 -29.4033 ; -20.9076 -17.6712
-0.5059
10.0696 5.2237 19.8114 -6.0140 -4.9942 -23.6393 5.0715 27.8773 -11.6181 -4.9215 18.1508
4.0674]

You might also like