I've been tasked with writing a K-Nearest Neighbor algorithm without using the built in MATLAB command, knnsearch().
I'm following the code provided here: http://www.cs.tut.fi/sgn/arg/SGN-2806/exer1/knn.m
To work out the distances, I'm using absolute differences.
My training data will look like this:
training data = [0 1 3 4 5 0 3 4 9 1 35 13 29 1 3]
Sample data to calculate knn, this:
inputdata = [1, 4, 5, 5, 2, 1 3]
This is what I am using to calculate absolute differences:
distancesbetweendata = abs(bsxfun(@minus, inputdata(1:N)), trainingdata(N-1));
This returns 0/empty
Is this correct?
In the code I'm following, the data is prepared such as:
N = size(sampleData,1); % N is length of data
M = size(sampleData,2); % M is index for class
However it uses data with multiple rows, whereas I am just using one.
How should I prepare my data in this instance?
Edit:
How to calculate output:
[sorted, idx] = sort(distance);
classes = sampleData(idx(1:min(N, k)));
resultClass = mode(classes); %OUTPUT