load OcrTrndataLiuSmall.mat;
data.X=X;
data.y=y;
disp_digits(data,16,16);
ocr_ins=[y' X'];

fprintf(1,'Now classifying the Wine dataset with %d instances using leave-one-out.\n', size(ocr_ins,1));
total_ins = 0;
total_correct = 0;
total_wrong = 0;
for ind=1:size(ocr_ins,1),
   ocr_ins_rest=ocr_ins([1:(ind-1) (ind+1):size(ocr_ins,1)],:);
   c1_ind=find(ocr_ins_rest(:,1)==1);
   ocr_ins_rest_c1=ocr_ins_rest(c1_ind,2:end);
   
   c2_ind=find(ocr_ins_rest(:,1)==2);
   ocr_ins_rest_c2=ocr_ins_rest(c2_ind,2:end);
   
   c3_ind=find(ocr_ins_rest(:,1)==3);
   ocr_ins_rest_c3=ocr_ins_rest(c3_ind,2:end);
   
   c4_ind=find(ocr_ins_rest(:,1)==4);
   ocr_ins_rest_c4=ocr_ins_rest(c4_ind,2:end);
   
   c5_ind=find(ocr_ins_rest(:,1)==5);
   ocr_ins_rest_c5=ocr_ins_rest(c5_ind,2:end);
   
   c6_ind=find(ocr_ins_rest(:,1)==6);
   ocr_ins_rest_c6=ocr_ins_rest(c6_ind,2:end);
   
   c7_ind=find(ocr_ins_rest(:,1)==7);
   ocr_ins_rest_c7=ocr_ins_rest(c7_ind,2:end);
   
   c8_ind=find(ocr_ins_rest(:,1)==8);
   ocr_ins_rest_c8=ocr_ins_rest(c8_ind,2:end);
   
   c9_ind=find(ocr_ins_rest(:,1)==9);
   ocr_ins_rest_c9=ocr_ins_rest(c9_ind,2:end);
   
   c0_ind=find(ocr_ins_rest(:,1)==10);
   ocr_ins_rest_c0=ocr_ins_rest(c0_ind,2:end);
   
   c1_mean = mean(ocr_ins_rest_c1);
   c2_mean = mean(ocr_ins_rest_c2);
   c3_mean = mean(ocr_ins_rest_c3);
   c4_mean = mean(ocr_ins_rest_c4);
   c5_mean = mean(ocr_ins_rest_c5);
   c6_mean = mean(ocr_ins_rest_c6);
   c7_mean = mean(ocr_ins_rest_c7);
   c8_mean = mean(ocr_ins_rest_c8);
   c9_mean = mean(ocr_ins_rest_c9);
   c0_mean = mean(ocr_ins_rest_c0);
   
   x=ocr_ins(ind,2:end);
   d1 = sum((x-c1_mean).^2);
   d2 = sum((x-c2_mean).^2);
   d3 = sum((x-c3_mean).^2);
   d4 = sum((x-c4_mean).^2);
   d5 = sum((x-c5_mean).^2);
   d6 = sum((x-c6_mean).^2);
   d7 = sum((x-c7_mean).^2);
   d8 = sum((x-c8_mean).^2);
   d9 = sum((x-c9_mean).^2);
   d0 = sum((x-c0_mean).^2);
   fprintf(1,'For instance %d (from class %d), the distance to class means are %8.6f %8.6f %8.6f.\n',...
      ind, ocr_ins(ind,1), d1,d2,d3);
   dists=[d1 d2 d3 d4 d5 d6 d7 d8 d9 d0];
   [dummy classified_ind]=min(dists);
   fprintf(1,'   using minimum distance classifier, it is classified as class %d, which is ',classified_ind);
   if classified_ind==ocr_ins(ind,1),
      fprintf(1,'correct.\n');
      total_correct = total_correct + 1;
   else
      fprintf(1,'wrong.\n');
      total_wrong = total_wrong + 1;
   end
   total_ins = total_ins + 1;
   %pause;
end
fprintf(1,'Using leave-one-out classification, %d are classified correctly, which is %5.2f%% classification accuracy.\n',...
   total_correct, (100.0*total_correct/total_ins));

