clear all
close all
clc
%%%% NN CON MEDIA O MEDIANA DE DISTANCIA....
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% data generation %%%% 
N1=500;
mu = [1 -1];
Sigma = [.9 .4; .4 .3];
c1 = mvnrnd(mu, Sigma, N1);
 plot(c1(:,1),c1(:,2),'ro');
 hold on
%%%%%%%%%%% 
 N2=10;
mu = [-4 3];
%mu = [-2 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);
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 x2=-3:0.2:7;
 x1=-7:0.2:4;
 %%%%%%%%%%%%%%%%%%%%%%%
 for k=1:length(x1)
     if mod(k,10)==0
        k
     end
     %%%%%%%%%%%%%%%%%%%%
    for j=1:length(x2)  
       %%%%%%%%%%%%%%%%%% 
         xtest=[x1(k),x2(j)];
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
            for i=1:N1
              d1(i)=dist(xtest,c1(i,:));
            end
         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
            for i=1:N2
              d2(i)=dist(xtest,c2(i,:));
            end
         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
        % d1av=mean(d1);
        % d2av=mean(d2);
         d1av=median(d1);
         d2av=median(d2);
        vaux=[d1av,d2av];
        dec=find(vaux==min(vaux));
          
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
          if  dec(1)==1
             plot(xtest(1,1),xtest(1,2),'r.','LineWidth',2,'MarkerSize',10)
          else
              plot(xtest(1,1),xtest(1,2),'b.','LineWidth',2,'MarkerSize',10)
         % else
          %    plot(xtest(1,1),xtest(1,2),'g.','LineWidth',2,'MarkerSize',10)
          end
    end
 end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


axis([-7 7 -3 8])
    