% This script is to plot the data set, test vectors and the decision line in tr% ansformed space.

% load the data set
%
load s1.dat;
load s2.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;
mean_1 = mean(b);
[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;
mean_2 = mean(c);

% compute the eigen values and eigen vectors
%
[ev1,eg1] = eig(cov(s1));
transform_1 = sqrtm(inv(eg1)) * transpose(ev1);
[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);


% Compute the points for plotting decision region in tranformed space
%
index = 1;
for i = -5:0.1:5,
   for j = -5:0.1:5,
      old_dist = dist(mean_4, transform_1*[i, j]');
      new_dist = dist(mean_5, transform_2*[i, j]');
      if (old_dist >= new_dist-0.1) 
         if (old_dist <= new_dist+0.1)
            transform_pts(index, 1) = i;
            transform_pts(index, 2) = j;
            index = index + 1;
         end;
      end;
   end;
end;

% plot the points for decision region in transformed space
%
grid on;
hold on;
box on;
plot(transform_pts(:,1),tranform_pts(:,2),'k-.');
plot(s1(:,1),s1(:,2),'k*');
plot(s2(:,1),s2(:,2),'k+');
plot(x1(1),x1(2),'kv');
plot(x2(1),x2(2),'kv');
plot(x3(1),x3(2),'kv');
plot(x4(1),x4(2),'kv');
legend('transformed decision region','set 1','set 2','test vectors');

% end of file
%
