%% Nearest 2-D Points
% Create a matrix P of 2-D data points and a matrix PQ of 2-D query points. Find the nearest data point to each query point, and compute the corresponding distances.

P = gallery('uniformdata',[10 2],0);
PQ = [0.5 0.5; 0.1 0.7; 0.8 0.7];
[k,dist] = dsearchn(P,PQ);

% Plot the data points and query points, and highlight the data point nearest to each query point.

plot(P(:,1),P(:,2),'ko')
hold on
plot(PQ(:,1),PQ(:,2),'*g')
hold on
plot(P(k,1),P(k,2),'*r')
legend('Data Points','Query Points','Nearest Points','Location','sw')

% Display the distances.

dist

% Copyright 2019 The MathWorks, Inc.