function ypred = algoKNN(Xtrain, ytrain, Xtest, k, t)

if(~exist('k','var'))
    k = 60;
end

if(~exist('t','var'))
    t = 0.25;
end

weights = (k:-1:1).^2;

Ztrain = bsxfun(@rdivide,Xtrain,sqrt(sum(Xtrain.*Xtrain,2)));
Ztest = bsxfun(@rdivide,Xtest,sqrt(sum(Xtest.*Xtest,2)));

dist = Ztrain*Ztest';
[~,I] = sort(dist,'descend');
ynear = ytrain(I(1,:),:)*weights(1);
ypred = ynear;
for i=2:k
    ypred = ypred+ytrain(I(i,:),:)*weights(i);
end
ypred = ypred/sum(weights);
ypred = ypred>t;

iz = sum(ypred,2)==0;
ypred(iz,:) = ynear(iz,:);
ypred = ypred>0;

%fprintf('%.0f %.0f ',k,t);

end