% function knn
function [I, hypo] = knn(fea_tr, lab_tr, fea_ts, lab_ts);
% train 1NN on fea_tr using fea_w

if nargin ~= 4
    disp('knn(fea_tr, lab_tr, fea_ts, lab_ts)');
    return;
end

vsize = size(fea_tr,1);
trnum = size(fea_tr,2);
tsnum = size(fea_ts, 2);
hypo = zeros(1, tsnum);

for n=1:tsnum
    d = fea_tr - repmat(fea_ts(:,n), 1, trnum);
    dist = sum(d.*d, 1);        % distance to each training data
    [value, index] = min(dist);
%    c = find(dist == min(dist(find(dist>0))))      % find minimun non-zero(notself) point index
    hypo(n) = lab_tr(index);
end

I = (hypo ~= lab_ts);
