KNN Classification | MATLAB
Code:-
clc
clear all
close all
warning off
x=input('Enter the first parameter');
y=input('Enter the second parameter:');
c=input('Enter the class');
k=input('Enter the k value');
a=input('Enter the first parameter value of the test acid:');
b=input('Enter the second parameter value of the test acid:');
distance=[];
for i=1:length(x)
e=sqrt((x(i)-a)^2+(y(i)-b)^2);
distance=[distance e];
end
temp=0;
gemp=0;
for i=1:length(distance)
for j=1:(length(distance)-i)
if(distance(j)>distance(j+1))
temp=distance(j);
distance(j)=distance(j+1);
distance(j+1)=temp;
gemp=c(j);
c(j)=c(j+1);
c(j+1)=gemp;
end
end
end
classy=[];
for i=1:k
classy=[classy c(i)];
end
output=mode(classy);
clc
clear all
close all
warning off
x=input('Enter the first parameter');
y=input('Enter the second parameter:');
c=input('Enter the class');
k=input('Enter the k value');
a=input('Enter the first parameter value of the test acid:');
b=input('Enter the second parameter value of the test acid:');
distance=[];
for i=1:length(x)
e=sqrt((x(i)-a)^2+(y(i)-b)^2);
distance=[distance e];
end
temp=0;
gemp=0;
for i=1:length(distance)
for j=1:(length(distance)-i)
if(distance(j)>distance(j+1))
temp=distance(j);
distance(j)=distance(j+1);
distance(j+1)=temp;
gemp=c(j);
c(j)=c(j+1);
c(j+1)=gemp;
end
end
end
classy=[];
for i=1:k
classy=[classy c(i)];
end
output=mode(classy);
No comments