Info

此问题已关闭。 请重新打开它进行编辑或回答。

how can ı determine classes in k-nn

2 次查看(过去 30 天)
mts
mts 2018-6-16
关闭: MATLAB Answer Bot 2021-8-20
My problem is as follows: I have one unknown value and two classes. I calculate the distances from an unknown value to every known value, and I make one matrix. But now I have to take the three smallest distances and I must determine their class, and I don't know how I can take these three values and determine their class and show it to the user
here is my code;
clc
clear;
clear all;
A=[1 2; 2 2; 1.5 3; 3 4];
B=[4 5; 7 5; 7 6; 9 7];
k=3;
x1=input('pls enter x ');
y1=input('pls enter y ');
for i=1:1:4
xa=A(i,1);
ya=A(i,2);
da=sqrt((x1-xa)^2+(y1-ya)^2);
for h=1:length(da)
danew(i)=da(h);
end
end
danew
for j=1:1:4
xb=B(j,1);
yb=B(j,2);
db=sqrt((x1-xb)^2+(y1-yb)^2);
for m=1:length(db)
dbnew(j)=db(m);
end
end
dbnew
dd=[danew dbnew]
for p=1:length(dd)
for r=length(dd):-1:1+p
if dd(r)<dd(r-1);
gecici=dd(r-1);
dd(r-1)=dd(r);
dd(r)=gecici;
end
end
end
dd

回答(1 个)

Walter Roberson
Walter Roberson 2018-6-16
If you use https://www.mathworks.com/help/stats/knnsearch.html#namevaluepairarguments with the 'K', 3 option to look for the three shortest distances, then what you get back is indices of points. You would use the indices of the points to access your list of classes.
(I suspect you are trying to do majority voting.)
  1 个评论
mts
mts 2018-6-17
thank you for help and yes ı am trying to do majority voting. But my problem is ı can not use functions of matlab, ı must to do it with 'for','if',etc.So ı must understand the algorithm of majority voting and ı must do it with simple code.So ı can take the three value but ı still cant find a way how ı can understand classes of these three values.

此问题已关闭。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by