function [y_out] = knn_euclidean(N_classes,x_train,y_train,x_test,k)
%KNN_EUCLIDEAN Summary of this function goes here
%   Detailed explanation goes here

N_test = size(x_test,1);
y_out = zeros(N_test,1);
un = zeros(N_test,N_classes);

[Idx,dist] = knnsearch(x_train,x_test,'K',k);
for j = 0:N_classes-1
    un(:,j+1) = sum(y_train(Idx)==j,2);
end

for i=1:N_test
    [M,I] = max(un(i,:));
    if (sum(un(i,:)==M) > 1)
        % we have a tie
        kn = k;
        [~,J] = sort(dist(i,:));
        tt = 1;
        while tt > 0
        kn = kn-1;
        for j = 0:N_classes-1
            un(i,j+1) = sum(y_train(Idx(i,J(1:kn)))==j);
        end;
        [M,I] = max(un(i,:));
        if (sum(un(i,:)==M) > 1)
            tt = 1;
        else
            tt = 0;
        end
        end
    end
    y_out(i) =  I-1;
end
end

