% Create the datasetn n=30; x=round(rand(n,1)*100); y=round(rand(n,1)*100); X=[x(:), y(:)]'; % Create the data point x=[40, 50]'; % Find the k nearest neighbors k=5; [index, distance]=knnSearch(x, X, k); % Plot the result plot(X(1,:), X(2, :), 'marker', '.', 'linestyle', 'none'); line(x(1), x(2), 'marker', '*'); line(X(1,index), X(2, index), 'marker', 'o', 'color', 'r', 'linestyle', 'none'); box on; axis image fprintf('x=%s\n', mat2str(x)); fprintf('X=%s\n', mat2str(X)); fprintf('k=%s\n', mat2str(k));