function y = nearest_neighbour(xtrain, xtest, t)
% calculate the nearest (single) neighbour classification
  
ntrain = size(xtrain,2); % number of training points
ntest = size(xtest,2); % number of test points

% Compute squared distances between vectors from the training and test sets 

% This is the obvious (but very slow way) to calculate distances :
for i = 1:ntrain
  for j = 1:ntest
    sqdist(i,j) = sum((xtrain(:,i)-xtest(:,j)).^2);
  end
end

% This is the super fast way (in MATLAB) to do this :
sqdist = repmat(sum(xtrain'.^2,2),1,ntest)+ ...
	 repmat(sum(xtest'.^2,2)',ntrain,1)-2*xtrain'*xtest;


[vals, kindex] = min(sqdist);
y = t(kindex);



