% This is the matlab code for classifying data in using principle components 
% analysis technique : homework #2. 
%

% Load the set1 data  created using xmgr
%
load s1.dat;
[m,n] = size(s1);

% Setup files for storing the results
%
FID = fopen('set1_original','w');

% adjust the data so that the required mean is obtained
%
for k=1:m
b(k,1) = s1(k,1) -1.2299;
b(k,2) = s1(k,2) +0.2579 ;
fprintf(FID,'%7.6f   %7.6f\n', b(k,1),b(k,2));
end;

% Calculate the mean of set1
%
mean_1 = mean(b);

% Load the set2 data created using xmgr
%
load s2.dat;
[m,n] = size(s2);

% Setup files for storing the results
%
FID = fopen('set2_original','w');

% adjust the data so that the required mean is obtained
%
for k=1:m
c(k,1) = s2(k,1) - 0.6351 ;
c(k,2) = s2(k,2) + 0.8489 ;   
fprintf(FID,'%7.6f   %7.6f\n', b(k,1),b(k,2));
end;

% Calculate the mean of set2
%
mean_2 = mean(c);

% Define the test set
%
x1=[-1,-1];
x2=[0,0];
x3=[0.5, 0.5];
x4=[0.5, -0.5];

% Calculate the euclidean distance in linear space
%
dist_1 = dist(x1,transpose(mean_1));
dist_2 = dist(x1,transpose(mean_2));
dist_3 = dist(x2,transpose(mean_1));
dist_4 = dist(x2,transpose(mean_2));
dist_5 = dist(x3,transpose(mean_1));
dist_6 = dist(x3,transpose(mean_2));
dist_7 = dist(x4,transpose(mean_1));
dist_8 = dist(x4,transpose(mean_2));


% Create a output file to store the results
%
output_file=fopen('output.txt','w');
fprintf(output_file,'******           Distance classification       *****\n');
fprintf(output_file,'******              in original space          *****\n\n');

% Compute the euclidean distance and classify the data
% based on the minimum distance
%
if (dist_1) < (dist_2) | (dist_1) == (dist_2)
   fprintf(output_file,'x1 belongs to set1 because the distance is %5.4f \n',dist_1);
else
   fprintf(output_file,'x1 belongs to set2 because the distance is %5.4f  \n',dist_2);
end;

if (dist_3) <= (dist_4)
   fprintf(output_file,'x2 belongs to set1 because the distance is %5.4f  \n',dist_3);
else
   fprintf(output_file,'x2 belongs to set2 because the distance is %5.4f  \n',dist_4);
end;

if (dist_5) <= (dist_6)
   fprintf(output_file,'x3 belongs to set1 because the distance is %5.4f  \n',dist_5);
else
   fprintf(output_file,'x3 belongs to set2 because the distance is %5.4f  \n',dist_6);
end;

if (dist_7) <= (dist_8)
   fprintf(output_file,'x4 belongs to set1 because the distance is %5.4f  \n',dist_7);
else
   fprintf(output_file,'x4 belongs to set2 because the distance is %5.4f  \n\n\n',dist_8);
end;

% Compute eigen values and eigen vectors for set1 and then calculate the 
% transformation matrix
%
[ev1,eg1] = eig(cov(s1));
transform_1 = sqrtm(inv(eg1)) * transpose(ev1);


% Compute eigen values and eigen vectors for set1 and then calculate the 
% transformation matrix
%
[v,d] = eig(cov(s2));
transform_2 = sqrtm(inv(d)) * transpose(v);

% Compute the new data in the transformed space
%
transformed_mat_1 = transpose(transform_1 * transpose(s1));
transformed_mat_2 = transpose(transform_2 * transpose(s2));

% Compute the mean of the transformed data
%
mean_4 = mean(transformed_mat_1);
mean_5 = mean(transformed_mat_2);

% Transform the test set into first transformed space
%
x1_transf = transpose(transform_1 * transpose(x1));
x2_transf = transpose(transform_1 * transpose(x2));
x3_transf = transpose(transform_1 * transpose(x3));
x4_transf = transpose(transform_1 * transpose(x4));

% Transform the test set into second transormed space
%
x1_2_transf = transpose(transform_2 * transpose(x1));
x2_2_transf = transpose(transform_2 * transpose(x2));
x3_2_transf = transpose(transform_2 * transpose(x3));
x4_2_transf = transpose(transform_2 * transpose(x4));

% Recompute the distances and reassign data
% 
new_dist_1 = dist(x1_transf,transpose(mean_4));
new_dist_2 = dist(x1_2_transf,transpose(mean_5));
new_dist_3 = dist(x2_transf,transpose(mean_4));
new_dist_4 = dist(x2_2_transf,transpose(mean_5));
new_dist_5 = dist(x3_transf,transpose(mean_4));
new_dist_6 = dist(x3_2_transf,transpose(mean_5));
new_dist_7 = dist(x4_transf,transpose(mean_4));
new_dist_8 = dist(x4_2_transf,transpose(mean_5));


fprintf(output_file,'******         New distance classification        *****\n\n');

% Compare the data and classify the test vectors
% 
if (new_dist_1) <= (new_dist_2)
   fprintf(output_file,'x1 belongs to set1 because the distance is %5.4f \n',new_dist_1);
else
   fprintf(output_file,'x1 belongs to set2 because the distance is %5.4f  \n',new_dist_2);
end;

if (new_dist_3) <= (new_dist_4)
   fprintf(output_file,'x2 belongs to set1 because the distance is %5.4f \n',new_dist_3);
else
   fprintf(output_file,'x2 belongs to set2 because the distance is %5.4f  \n',new_dist_4);
end;
 
if (new_dist_5) <= (new_dist_6)
   fprintf(output_file,'x3 belongs to set1 because the distance is %5.4f \n',new_dist_5);
else
   fprintf(output_file,'x3 belongs to set2 because the distance is %5.4f \n',new_dist_6);
end;

if (new_dist_7) <= (new_dist_8)
   fprintf(output_file,'x4 belongs to set1 because the distance is %5.4f \n',new_dist_7);
else
   fprintf(output_file,'x4 belongs to set2 because the distance is %5.4f  \n',new_dist_8);
end;

% Compute the covariance of tranformed data 
%
cov_identity_1 = cov(transformed_mat_1);
cov_identity_2 = cov(transformed_mat_2);


% shift the training sets to the origin and compute the transformed sets
%
for i=1:length(s1)
	shift_set1(i,:)=(s1(i,:) - mean_1(1,:));
end;
shift_set1_transf = transpose(transform_1*transpose(shift_set1));

for i=1:length(s2)
	shift_set2(i,:)=s1(i,:)-mean_2(1,:);
end;
shift_set2_transf = transpose(transform_2*transpose(shift_set2));

% close all the ourpur files
%
fclose(output_file);
fclose('all');

% end of file

