 clear all;
 close all;

 %readclassdatafiles
 hw1=csvread(hardwood.csv);
 cp1=csvread(carpet.csv);

 %selectfeature48and49
 x1=[hw1(:,48);cp1(:,48)];
 x2=[hw1(:,49);cp1(:,49)];

 % center them w.r.t their mean
 x1=x1-mean(x1);
 x2=x2-mean(x2);


 % concatenate them
 xx=[x1;x2];
 % plot the classes
 figure;plot(xx(1,1:1024),xx(2,1:1024),.);grid on;
 hold on;plot(xx(1,1025:end),xx(2,1025:end),r.);grid on;

 % calculate covariance matrix
 C=(xx*xx)/(length(xx)-1);


 % calculate eignvectors and eigenvalue of C
 [ww,ee]=eig(C);


 % get eigenvalues
 ss=sum(ee);


 %validate eigenvector equation
 C*ww(:,1)
 ss(1)*ww(:,1)

 C*ww(:,2)
 ss(2)*ww(:,2)

 %validation ends


 % plot and show the principal directions
 hold on;line([0 30*ww(1,2)],[0 30*ww(2,2)],color,black,LineWidth,3);
 hold on;line([0 -30*ww(1,2)],[0 -30*ww(2,2)],color,black,LineWidth,3);
 hold on;line([0 30*ww(1,1)],[0 30*ww(2,1)],color,black,LineWidth,3);


 % transform to a new space using principal components
 yy=ww*xx;


 % plot the classes in the new space
 figure;plot(yy(1,1:1024),yy(2,1:1024),.);grid on;
 hold on;plot(yy(1,1025:end),yy(2,1025:end),r.);grid on;