www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/NearestNeighborsOfQueryPointsExample.m

    %% Determine Nearest Neighbors of Query Points
% 
%%
% Define the points and connectivity of the triangulation.

% Copyright 2015 The MathWorks, Inc.

P = [2.5 8.0; 6.5 8.0; 2.5 5.0; 6.5 5.0; 1.0 6.5; 8.0 6.5];
T = [5 3 1; 3 2 1; 3 4 2; 4 6 2];
TR = triangulation(T,P);

%%
% Define two query points.
QP = [2 4; 6 6.5];

%%
% Plot the triangulation and the query points.
triplot(TR)
hold on
plot(QP(:,1),QP(:,2),'r*')
ylim([1.5 8.5])
xlim([0.5 8.5])

%%
% Find the nearest neighbors to the query points, as well as the distances
% between them.
[vi,d] = nearestNeighbor(TR,QP);

%%
% Highlight the points in the triangulation that are the nearest neighbors
% to the query points.
TP = P(vi,:);
plot(TP(:,1),TP(:,2),'ks')

%%
% Examine the distance between each query point and its nearest neighbor.
d