zip_train_sub;
data.X=zip_ins(:,2:end)';
data.y=zip_ins(:,1)';
disp_digits1(data,16,16);
ocr_ins=zip_ins;
fprintf(1,'Now classifying the ocr 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)==0);
   ocr_ins_rest_c0=ocr_ins_rest(c0_ind,2:end);
   

   c1_mean = mean(ocr_ins_rest_c1);
   c1_covar = cov(ocr_ins_rest_c1);
   c1_covar = diag(diag(c1_covar)+0.0001);
   
   c2_mean = mean(ocr_ins_rest_c2);
   c2_covar = cov(ocr_ins_rest_c2);
   c2_covar = diag(diag(c2_covar)+0.0001);
   
   c3_mean = mean(ocr_ins_rest_c3);
   c3_covar = cov(ocr_ins_rest_c3);
   c3_covar = diag(diag(c3_covar)+0.0001);
      
   c4_mean = mean(ocr_ins_rest_c4);
   c4_covar = cov(ocr_ins_rest_c4);
   c4_covar = diag(diag(c4_covar)+0.0001);
   
   c5_mean = mean(ocr_ins_rest_c5);
   c5_covar = cov(ocr_ins_rest_c5);
   c5_covar = diag(diag(c5_covar)+0.0001);
   
   c6_mean = mean(ocr_ins_rest_c6);
   c6_covar = cov(ocr_ins_rest_c6);
   c6_covar = diag(diag(c6_covar)+0.0001);
   
   c7_mean = mean(ocr_ins_rest_c7);
   c7_covar = cov(ocr_ins_rest_c7);
   c7_covar = diag(diag(c7_covar)+0.0001);
   
   c8_mean = mean(ocr_ins_rest_c8);
   c8_covar = cov(ocr_ins_rest_c8);
   c8_covar = diag(diag(c8_covar)+0.0001);
   
   c9_mean = mean(ocr_ins_rest_c9);
   c9_covar = cov(ocr_ins_rest_c9);
   c9_covar = diag(diag(c9_covar)+0.0001);

      
   c0_mean = mean(ocr_ins_rest_c0);
   c0_covar = cov(ocr_ins_rest_c0);
   c0_covar = diag(diag(c0_covar)+0.0001);


   x=ocr_ins(ind,2:end);
   g1 = -0.5 * (x-c1_mean)*inv(c1_covar)*(x-c1_mean)'-0.5*sum(log(diag(c1_covar)));
   g2 = -0.5 * (x-c2_mean)*inv(c2_covar)*(x-c2_mean)'-0.5*sum(log(diag(c2_covar)));
   g3 = -0.5 * (x-c3_mean)*inv(c3_covar)*(x-c3_mean)'-0.5*sum(log(diag(c3_covar)));
   g4 = -0.5 * (x-c4_mean)*inv(c4_covar)*(x-c4_mean)'-0.5*sum(log(diag(c4_covar)));
   g5 = -0.5 * (x-c5_mean)*inv(c5_covar)*(x-c5_mean)'-0.5*sum(log(diag(c5_covar)));
   g6 = -0.5 * (x-c6_mean)*inv(c6_covar)*(x-c6_mean)'-0.5*sum(log(diag(c6_covar)));
   g7 = -0.5 * (x-c7_mean)*inv(c7_covar)*(x-c7_mean)'-0.5*sum(log(diag(c7_covar)));
   g8 = -0.5 * (x-c8_mean)*inv(c8_covar)*(x-c8_mean)'-0.5*sum(log(diag(c8_covar)));
   g9 = -0.5 * (x-c9_mean)*inv(c9_covar)*(x-c9_mean)'-0.5*sum(log(diag(c9_covar)));
   g0 = -0.5 * (x-c0_mean)*inv(c0_covar)*(x-c0_mean)'-0.5*sum(log(diag(c0_covar)));

   fprintf(1,'For instance %d (from class %d), the discriminant functions are ',...
      ind, ocr_ins(ind,1));
   disc_funcs=[g0 g1 g2 g3 g4 g5 g6 g7 g8 g9];
   fprintf(1,'%8.6f ', disc_funcs);
   fprintf(1,'.\n');
   [dummy classified_ind]=max(disc_funcs);
   classified_ind = classified_ind-1;
   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, there are %d correct out of %d, which is %8.6f %%.\n',...
   total_correct, total_ins, (100.0*total_correct/total_ins));

