fork download
  1. clc;clear all;close all;
  2.  
  3. %% STEP1 Load dataset
  4. load DataSet
  5.  
  6. %% STEP2 Raw Data Visualization
  7.  
  8. plot(Data(:,1), Data(:,2), 's')
  9.  
  10. %% STEP3 Reference Data Preparation
  11. ii=1; JJ=1;
  12. for k=1:length(Data)
  13. if Data(k,3) ==1
  14. dog(i,:)=Data(k,:)
  15. ii=ii+1;
  16. else
  17. cat(jj,:)=Data(k,:)
  18. jj=jj+1;
  19.  
  20. %STEP 4 Visualization
  21. plot(dog(:,1),dog(:,2),'r*')
  22. hold on;
  23. plot(cat(:,1), cat(:,2), 'o');
  24.  
  25. % Text Labeling
  26. d1bar=sum(dog(:,1))/length(dog);
  27. d2bar=sum(dog(:,2))/length(dog);
  28. c1bar=sum(cat(:,1))/length(cat);
  29. c2bar=sum(cat(:,2))/length(cat);
  30. text(d1bar,d2bar,'Dog','FontSize',20)
  31. text(c1bar,c2bar,'Cat','FontSize',20)
  32. xlabel('X1','FontSize',20)
  33. ylabel('X2','FontSize',20)
  34. title('K-Nearest Neighbor','FontSize',15)
Success #stdin #stdout 0.02s 25884KB
stdin
Standard input is empty
stdout
clc;clear all;close all;

%%  STEP1 Load dataset
load DataSet

%% STEP2 Raw Data Visualization

plot(Data(:,1), Data(:,2), 's')

%% STEP3 Reference Data Preparation
ii=1; JJ=1;
for k=1:length(Data)
    if Data(k,3) ==1
        dog(i,:)=Data(k,:)
        ii=ii+1;
    else
        cat(jj,:)=Data(k,:)
        jj=jj+1;
    end
end

%STEP 4 Visualization
	plot(dog(:,1),dog(:,2),'r*')
	hold on;
	plot(cat(:,1), cat(:,2), 'o');

	% Text Labeling
	d1bar=sum(dog(:,1))/length(dog);
	d2bar=sum(dog(:,2))/length(dog);
	c1bar=sum(cat(:,1))/length(cat);
	c2bar=sum(cat(:,2))/length(cat);
	text(d1bar,d2bar,'Dog','FontSize',20)
	text(c1bar,c2bar,'Cat','FontSize',20)
	xlabel('X1','FontSize',20)
	ylabel('X2','FontSize',20)
	title('K-Nearest Neighbor','FontSize',15)