load fisheriris;
n = size(meas,1);
%suffle entries
P = randperm(n);
X = meas(P,:);
Y = species(P);
% create a vector of ~90% random ones
TR = binornd(ones(n,1),0.9);
% filter the sample (X,Y) to a training sample (XTR, YTR)
% and a test sample (XTS, YTS)
XTR = X(TR==1,:);
XTS = X(TR==0,:);
YTR = Y(TR==1);
YTS = Y(TR==0);
% pick a random test sample
ti = random('unid',size(XTS,1));
ts = XTS(ti,:);
ys = YTS(ti);
% D(i,:) is XTR(i,:) - ts
D = XTR - repmat(ts, size(XTR,1), 1);
% i is the index of the minimal squared distance
[~,i] = min(sum(D.^2,2));
% print 1 if the nearest neighbor label is equal to the test label
strcmp(ys,YTR(i))