function [class] = simplekNN(test, db, k) % Classify the point 'test' in the following way: % 1. find k nearest neighbours. % 2. find the class the max number of nearest neighbors are from. % 3. classify 'test' as being from that class. % Inputs: % 1. test: vector length m % 2. db : n*(m+1) matrix. last column states class that the vector % belongs to (0 or 1) % 3. k: number of nearest neaighbours % Output: which class the object belongs to (2 or 1) numObjects = size(db,1); numCols = size(db, 2); [cnn indexCount] = kNN(test, db(:,1:(numCols-1)), k); count1 = 0; count2 = 0; for i=1:length(indexCount) c = db(indexCount(i),numCols); if(c == 1) count1 = count1 + 1; else count2 = count2 + 1; end end if(count1 > count2) class = 1; else class = 2; end