clear all
close all
clc
%%%% NN-tradicional
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% data generation %%%% 
N1=1000;
mu = [1 -1];
Sigma = [.9 .4; .4 .3];
c1 = mvnrnd(mu, Sigma, N1);
 plot(c1(:,1),c1(:,2),'ro');
 hold on
%%%%%%%%%%% 
 N2=1000;
%mu = [-4 3];
mu = [-1 1.5];
%mu = [0 1.5];
Sigma = [1 0; 0 2];
c2 = mvnrnd(mu, Sigma, N2);
 plot(c2(:,1),c2(:,2),'bo');
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 dist=@(z,x) sum(abs(x-z).^2);
 Kvecinos=900;
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
axis([-7 7 -3 8])
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% calculaErrores
Ctot=[c1; c2];
Labels_Ctot=[ones(1,N1) zeros(1,N2)];
%%%%%%%%%%%%%%%%%%%%%%
Cont_Empate=0;
Cont_Correcto1=0;
Cont_Error1=0;
Cont_Correcto2=0;
Cont_Error2=0;
%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%
for k=1:N1+N2
     if mod(k,10)==0
        k
     end
    
         xtest=Ctot(k,:);
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
            for i=1:N1
              d1(i)=dist(xtest,c1(i,:));
            end
         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
            for i=1:N2
              d2(i)=dist(xtest,c2(i,:));
            end
         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
           dtot=[d1 d2];
           labels=[ones(1,N1) zeros(1,N2)];
           dsort=sort(dtot);
         %%%%%%%%%%%%%%%%%  
           contneg=0;
           contpos=0;
         %%%%%%%%%%%%%%%%%
           for s=1:Kvecinos
               pos(s)=find(dsort(s)==dtot);
               Lab(s)=labels(pos(s));
               if Lab(s)==1
                   contpos=contpos+1;
               else
                   contneg=contneg+1;
               end
           end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
          if  contpos>contneg
              dec=1;
              if Labels_Ctot(k)==1
                 Cont_Correcto1=Cont_Correcto1+1;
              else
                  Cont_Error1=Cont_Error1+1;
                  plot(xtest(1,1),xtest(1,2),'gx','LineWidth',2,'MarkerSize',10)
              end
          %%%%%%%%%%%%%%%%%%%%%%%%%%% 
          elseif contpos<contneg
          %%%%%%%%%%%%%%%%%%%%%%%%%%%    
              dec=0;
                if Labels_Ctot(k)==0
                 Cont_Correcto2=Cont_Correcto2+1;
              else
                  Cont_Error2=Cont_Error2+1;
                  plot(xtest(1,1),xtest(1,2),'kx','LineWidth',2,'MarkerSize',10)
              end
         %%%%%%%%     
          else
         %%%%%%%%   
         contpos
         contneg
             Cont_Empate=Cont_Empate+1;
             plot(xtest(1,1),xtest(1,2),'cx','LineWidth',2,'MarkerSize',10)
             pause 
          end
end

    Cont_Error1
    Cont_Error2
    Cont_Empate