# Knn Classification MATLAB

I have a knn classifier that finds the k nearest neighbors of the given data. While classification I am not able to handle ties. I want to handle ties in the following way:

If there is only one class and it is correct class accuracy is 1

If there is only one class and it is incorrect class accuracy is 0

If there is more than one class tied for best and the correct class is one of those then accuracy = 1/no_of_classes_tied_for_best.

How do I do this in Matlab?

Code till now: test has my knnclassify results for 5 neighbors

classes_test has my verification class.

for i = 1:size(test,1)
set = 0;
for j = 1:size(test,2)
if test(i,j) == classes_test(i,1) && set~=1
set = 1;
accuracy = accuracy + 1;
end
end
end

• I don't see how your plan handles ties. Maybe you can edit the post and make it more clear. – Michael Chernick Apr 14 '17 at 18:05
• That is what I am not able to figure out – Kobe00992 Apr 14 '17 at 18:35

When using kNN, $k$ should always be set to odd numbers in order to avoid ties (3,5,7,9,11...). This is standard knowledge in machine learning.