wine_data;
for ccol=2:size(wine_ins,2),
   minV=min(wine_ins(:,ccol));
   maxV=max(wine_ins(:,ccol));
   varCol=var(wine_ins(:,ccol));
   wine_ins(:,ccol) = (wine_ins(:,ccol)-minV)/sqrt(varCol);
end

wine_ins_all = wine_ins;
selected=[];
not_selected=[2:size(wine_ins_all,2)];
while length(not_selected) > 0,
   not_selected_perf=[];
   for not_sel_ind=1:length(not_selected),
      ccol = not_selected(not_sel_ind);
      wine_ins=wine_ins_all(:,[1 selected ccol]);
      fprintf(1,'Now classifying the Wine dataset with %d instances using leave-one-out.\n', size(wine_ins,1));
      total_ins = 0;
      total_correct = 0;
      total_wrong = 0;
      for ind=1:size(wine_ins,1),
         wine_ins_rest=wine_ins([1:(ind-1) (ind+1):size(wine_ins,1)],:);
         c1_ind=find(wine_ins_rest(:,1)==1);
         wine_ins_rest_c1=wine_ins_rest(c1_ind,2:end);
         c2_ind=find(wine_ins_rest(:,1)==2);
         wine_ins_rest_c2=wine_ins_rest(c2_ind,2:end);
         c3_ind=find(wine_ins_rest(:,1)==3);
         wine_ins_rest_c3=wine_ins_rest(c3_ind,2:end);
         c1_mean = mean(wine_ins_rest_c1);
         c1_covar = cov(wine_ins_rest_c1);
         c2_mean = mean(wine_ins_rest_c2);
         c2_covar = cov(wine_ins_rest_c2);
         c3_mean = mean(wine_ins_rest_c3);
         c3_covar = cov(wine_ins_rest_c3);
         x=wine_ins(ind,2:end);
         g1 = -0.5 * (x-c1_mean)*inv(c1_covar)*(x-c1_mean)'-0.5*log(det(c1_covar));
         g2 = -0.5 * (x-c2_mean)*inv(c2_covar)*(x-c2_mean)'-0.5*log(det(c2_covar));
         g3 = -0.5 * (x-c3_mean)*inv(c3_covar)*(x-c3_mean)'-0.5*log(det(c2_covar));
         %fprintf(1,'For instance %d (from class %d), the discriminant functions are %8.6f %8.6f %8.6f.\n',...
         %   ind, wine_ins(ind,1), g1,g2,g3);
         disc_funcs=[g1 g2 g3];
         [dummy classified_ind]=max(disc_funcs);
         if classified_ind==wine_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,'For feature %d, using leave-one-out classification, there are %d correct out of %d, which is %8.6f %%.\n',...
         ccol, total_correct, total_ins, (100.0*total_correct/total_ins));
      not_selected_perf=[not_selected_perf (100.0*total_correct/total_ins)];
   end
   [dummy next_feature_ind]=max(not_selected_perf);
   next_feature=not_selected(next_feature_ind);
   selected=[selected next_feature];
   
   not_selected=setdiff(not_selected,next_feature);
   fprintf(1,'Next added feature is %d (%d chosen so far) with performance %8.6f %%.\n', ...
      next_feature, length(selected), dummy);
   if length(not_selected) > 0,
      pause;
   end
end

