function [Yu] = k_NearestNeighbor(Yl, D, k)
% kNN for binary classification.
%
% Input:
%   Yl -- known labels
%   D -- distances of labeled and unlabeled points
%   k -- k in k-NN
% Output:
%   Yu -- predicted labels
%
% (c) 2011-2016 Zalan Bodo

Nl = size(Yl, 1);
Nu = size(D, 1) - Nl;

D = D(1:Nl, Nl+1:end);

[tmp, ind] = sort(D);
for i = 1:size(D,2)
    D(ind(1:k,i),i) = 1;
    D(ind(k+1:end,i),i) = 0;
end

c1 = sum(repmat((Yl == -1), [1 Nu]) .* D);
c2 = sum(repmat((Yl == 1), [1 Nu]) .* D);

[m, ind] = max([c1; c2]);
Yu = (2*(ind-1) - 1)';
