% Nearest Neighbour demo : 3 classes

xtrain(:,1:10) = randn(2,10); % 10 two-dimesional training points
label(1,1:10)=1; % class one
xtrain(:,11:20) = randn(2,10)+repmat([2 2.5]',1,10); % 10 two-dimesional training points
label(1,11:20)=2; % class two
xtrain(:,21:30) = randn(2,10)-repmat([2 2.5]',1,10); % 10 two-dimesional training points
label(1,21:30)=3; % class three

% code the training data :
plot(xtrain(1,1:10),xtrain(2,1:10),'r.');text(xtrain(1,1:10),xtrain(2,1:10),'1'); hold on
plot(xtrain(1,11:20),xtrain(2,11:20),'b.');text(xtrain(1,11:20),xtrain(2,11:20),'2')
plot(xtrain(1,21:30),xtrain(2,21:30),'g.');text(xtrain(1,21:30),xtrain(2,21:30),'3')

% now find the class for a bunch of novel points xquery
xquery = 2.5*randn(2,15) + repmat([1.5 0.5]',1,15);

nn_label = nearest_neighbour(xtrain, xquery, label)
plot(xquery(1,:),xquery(2,:),'k.')

nn_label1=find(nn_label==1);nn_label2=find(nn_label==2);nn_label3=find(nn_label==3);
text(xquery(1,nn_label1),xquery(2,nn_label1),'1'); 
text(xquery(1,nn_label2),xquery(2,nn_label2),'2'); 
text(xquery(1,nn_label3),xquery(2,nn_label3),'3'); hold off


