% Kristiyan Georgiev
% A.I. , 12 Oct 2010
% K-Nearest Neighbor Classifier, works with multiple classes

function res = knnC(trainData, trainLabel, testData, testLabel, k)
numData = size(trainData,1);
numPredictions = size(testData,1);

for i=1:numPredictions
    dist = sum((repmat(testData(i,:), numData, 1) - trainData).^2, 2);
    [sortval sortpos] = sort(dist);
    res.possition(i,:) = sortpos(1:k);
    res.distance(i,:) = sqrt(sortval(1:k));
    res.class(i,:) = trainLabel(sortpos(1:k),1);
end
classList = unique(res.class);
cSize = [];
for i=1:numPredictions    
    cSize = [];
    for j=1:size(classList)
        selectedClass = classList(j);
        thisClassCount  = size(find(res.class(i,:)==selectedClass),2);
        cSize = [cSize; [selectedClass thisClassCount] ];
    end
    maxClassIndex = find(cSize(:,2)==max(cSize(:,2))); 
    res.finalclass(i) = classList(maxClassIndex(1)); % max of cSize
end
res.correct = sum(res.finalclass' == testLabel); 
res.error =  1- res.correct / numPredictions;
end
