% in this script I assume you have X and Y matrices obtained from data
% preparation
% X is of size nx2, and contains the green and red components of the mean
% values of normalized colors
% Y is of size nx1 and contains the labels associated to X: 1 for
% chrysanthemum, 2 for carnation, 3 for pansy

% Prepare the grid of values to be classified later on
r_min = 0.35; r_max = 0.6; r = r_min:0.0025:r_max;
g_min = 0.2;  g_max = 0.45; g = g_min:0.0025:g_max;
[g_mat, r_mat] = meshgrid(g_min:0.0025:g_max, r_min:0.0025:r_max);
grid_coord = [g_mat(:) r_mat(:)];

% TO BE COMPLETED
% Nearest Neighbour search using the function knnsearch


% You should obtain a variable knn_class of size 101x101 that contains the
% class of every pixel in the considered space, as computed by the knn
% classifier


% plotting the result nicely
figure;
surface(g,r,knn_class); hold on;
carte_couleurs = [.65 .45 .45 ; .45 .65 .45 ; .45 .45 .65];
colormap(carte_couleurs);
axis square
plot3(X(Y==1,1),X(Y==1,2),4*ones(sum(Y==1),1),'r*','MarkerSize',10,'LineWidth',2);
plot3(X(Y==2,1),X(Y==2,2),4*ones(sum(Y==2),1),'g*','MarkerSize',10,'LineWidth',2);
plot3(X(Y==3,1),X(Y==3,2),4*ones(sum(Y==3),1),'b*','MarkerSize',10,'LineWidth',2);
xlabel('normalized g')
ylabel('normalized r')
title('K-nearest neighbors classification');