I'm trying to simulate 3 nearest neighbour classification without using the builtin matlab functions. I was able to 1 nearest neigbhor however i am unable to extend it to more

%% code for 1 nearest neighbor classification
total_num_test_cases = num_test_cases * 5; %75
total_num_train_cases = num_train_cases * 5; %175
% Create a vector to store assigned labels
inferredLabels = zeros(1, total_num_test_cases);
% This loop cycles through each unlabelled item:
for unlabelledCaseIdx = 1:total_num_test_cases
unlabelledCase = testingSet(unlabelledCaseIdx, :);
% As any distance is shorter than infinity
shortestDistance = inf;
shortestDistanceLabel = 0; % Assign a temporary label
% This loop cycles through each labelled item:
for labelledCaseIdx = 1:total_num_train_cases
labelledCase = trainingSet(labelledCaseIdx, :);
% Calculate the Euclidean distance:
currentDist = euc(unlabelledCase, labelledCase);
% Check the distance
if currentDist < shortestDistance
shortestDistance = currentDist;
shortestDistanceLabel = trainingTarget(labelledCaseIdx);
end
end % inner loop
% Assign the found label to the vector of inferred labels:
inferredLabels(unlabelledCaseIdx) = shortestDistanceLabel
end % outer loop

 Accepted Answer

Attach your test and training sets of data. You can get the distances of a point to all other points in one line of code. Then sort it in ascending order.
distances = sqrt((x - allX) .^ 2 + (y - allY) .^ 2);
[sortedDistances, sortOrder] = sort(distances)
indexesOfClosest3 = sortOrder(1:3)

5 Comments

num_train_cases=35;
num_test_cases=length(F1)-num_train_cases;
Unrecognized function or variable 'F1'.
trainingSet=[F1(1:num_train_cases,:);
F2(1:num_train_cases,:);
F3(1:num_train_cases,:);
F4(1:num_train_cases,:);
F5(1:num_train_cases,:)]
testingSet=[F1(num_train_cases+1:end,:);
F2(num_train_cases+1:end,:);
F3(num_train_cases+1:end,:);
F4(num_train_cases+1:end,:);
F5(num_train_cases+1:end,:)]
trainingTarget=[ones(1,num_train_cases), ones(1,num_train_cases)*2, ones(1,num_train_cases)*3, ones(1,num_train_cases)*4, ones(1,num_train_cases)*5];
testingTarget=[ones(1,num_test_cases), ones(1,num_test_cases)*2, ones(1,num_test_cases)*3, ones(1,num_test_cases)*4, ones(1,num_test_cases)*5];
This is my code for test and training sets. F1 to F5 are some matrices that i have evaluated and cannot mention those because it will become complicated then.
Did you try my suggestion?
What do you want? Do you want for every point in some array, the indexes of the 3 other points that are closest to it? So the result would be like an N-by-3 array of indexes?
Yes i want that three closest points and their arrays so that i can store them and then compare it to classify as 3 nearest neighbor
OK so I interpret that as you don't want to try my solution yourself and just want me to do it for you. Here it is:
numPoints = 30;
allX = 100 * rand(1, numPoints);
allY = 100 * rand(1, numPoints);
plot(allX, allY, 'b.', 'MarkerSize', 40);
grid on;
hold on;
drawnow;
indexesOfClosest3 = zeros(numPoints, 3);
for k = 1 : numPoints
x = allX(k);
y = allY(k);
distances = sqrt((x - allX) .^ 2 + (y - allY) .^ 2);
[sortedDistances, sortOrder] = sort(distances);
indexesOfClosest3(k, :) = sortOrder(2:4); % Don't include the first distance which is zero (the distance of the point to itself).
% Plot lines to them
for k3 = 1 : 3
x3 = allX(indexesOfClosest3(k, k3));
y3 = allY(indexesOfClosest3(k, k3));
line([x, x3], [y, y3], 'Color', 'r', 'LineWidth', 2)
end
end
Note every point is connected to the 3 other closest points. Is that what you want?

Sign in to comment.

More Answers (0)

Products

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!