 clear all;
 close all;

 hw1=csvread(hardwood.csv);
 cp1=csvread(carpet.csv);

%Iterationoverthefeatures
 tw1=hw1(:,1:1:end-1); tw2=hw1(:,2:1:end);
 hw=sqrt(tw1.*tw1+tw2.*tw2);

 tp1=cp1(:,1:1:end-1); tp2=cp1(:,2:1:end);
 cp=sqrt(tp1.*tp1+tp2.*tp2);


% Labels are added
 yy=[ones(1,size(hw,1)) zeros(1,size(cp,1))];
 xx=[hw;cp];


% Finding the best split and the best feature

% using information gain
 for ii=1:size(xx,1)
   sig(ii)=std(xx(ii,:));
   [slx(ii),fmx(ii),igx{ii},md1(ii),md0(ii),n1(ii),n2(ii), \ldots
   o1(ii),o2(ii)]=ura_calc_ig_fn(xx(ii,:),yy,sig(ii));
 end

 fmxsort=sort(fmx,descend);
 tmp1=find(fmx==fmxsort(1));
 f1=tmp1(1);
 fval=xx(f1,slx(f1));


% Splitting the circular domain
 idxL=find(xx(f1,:)<fval);
 idxR=find(xx(f1,:)>=fval+0.5*sig(f1));
 idxM=find(xx(f1,:)>=fval & xx(f1,:)<fval+0.5*sig(f1));

 xxL=xx(:,idxL);
 xxR=xx(:,idxR);
 xxM=xx(:,idxM);

 yyL=yy(idxL);
 yyR=yy(idxR);
 yyM=yy(idxM);


% Showing the classification counts
 fprintf(\n);
 fprintf(Left side class 1 = %d\n,sum(yyL));
 fprintf(Left side class 0 = %d\n\n,length(yyL)-sum(yyL));

 fprintf(Middle class 1 = %d\n,sum(yyM));
 fprintf(Middle class 0 = %d\n\n,length(yyM)-sum(yyM));

 fprintf(Right side class 1 = %d\n,sum(yyR));
 fprintf(Right side class 0 = %d\n,length(yyR)-sum(yyR));


% Showing the classes using circles
 figure;
 for ii=1:size(xx,2)
   if yy(ii)==1
     circle(0,0,xx(f1,ii),.,0.01);
   else
     circle(0,0,xx(f1,ii),r.,0.09);
   end
   hold on;grid on;
 end


% Showing the maximum IG for each feature
 figure;plot(md1);hold on;plot(md1,.);grid on;
 hold on;plot(md0,color,r);hold on;plot(md0,r.);

 figure;plot(fmx);grid on;
 xlabel(Feature number);ylabel(Information gain);

% Showing the features 1 and 2
 figure;plot(hw1(:,1),hw1(:,2),.);
 hold on;plot(cp1(:,1),cp1(:,2),r.);
 axis([40 240 40 240]);grid on;
 xlabel(Feature 1);ylabel(Feature 2);

 jj=48;

% Showing the best features 48 and 49
 figure;plot(hw1(:,jj),hw1(:,jj+1),.);
 hold on;plot(cp1(:,jj),cp1(:,jj+1),r.);
 axis([40 240 40 240]);grid on;
 xlabel(Feature 48);ylabel(Feature 49);

 quartcircle(0,0,fval,m.,0.001);
 quartcircle(0,0,fval+0.5*sig(jj),m.,0.001);


%%%%%%%%%%%%%%
 idxx=find(xx(f1,:)>=fval & xx(f1,:)<fval+0.5*sig(jj));
 hww1=hw1(idxx(idxx<=1024),:);
 cpp1=cp1(idxx(idxx>1024)-1024,:);


% figure;plot(hww1(:,jj),hww1(:,jj+1),.);

% hold on;plot(cpp1(:,jj),cpp1(:,jj+1),r.);grid on;


% Unit Ring Domain Division
 tww1=hww1(:,1:1:end-1); tww2=hww1(:,2:1:end);
 hww=tww2./tww1;

 tpp1=cpp1(:,1:1:end-1); tpp2=cpp1(:,2:1:end);
 cpp=tpp2./tpp1;

 wyy=[ones(1,size(hww,1)) zeros(1,size(cpp,1))];
 wxx=[hww;cpp];

 uu=(max(wxx(:,jj))-min(wxx(:,jj)))/9;
 for tt=min(wxx(:,jj))-0.05:uu:max(wxx(:,jj))+0.05
   iddx=find((tt<wxx(:,jj) & wxx(:,jj)<tt+uu));
   s1=sum(wyy(iddx));
   l1=length(wyy(iddx));
   fprintf((%4.2f,%4.2f),(%4.2f,%4.2f),\n,s1,(l1-s1)\ldots113 ,s1/l1,(l1-s1)/l1);
 end

 m1=0;
 m2=max(cpp1(:,jj))+40;

 for tt=min(wxx(:,jj))-0.05:uu:max(wxx(:,jj))+uu
   tt
   hold on;line([m1:m2],tt*[m1:m2]);
   axis([0 200 0 200]);
 end
