www.gusucode.com > stats 源码程序 matlab案例代码 > stats/ReturnDistancesOfNeighborsWithinARadiusExample.m
%% Compute Distances of Neighbors Within a Radius %% % Load Fisher's iris data set. % Copyright 2015 The MathWorks, Inc. load fisheriris %% % Remove five irises randomly from the predictor data to use as a query set. rng(4); % For reproducibility n = size(meas,1); % Sample size qIdx = randsample(n,5); % Indices of query data X = meas(~ismember(1:n,qIdx),:); Y = meas(qIdx,:); %% % Grow a four-dimensional _K_ d-tree using the training data. Specify to use the Minkowski % distance for finding nearest neighbors later. Mdl = KDTreeSearcher(X); %% % |Mdl| is a |KDTreeSearcher| model. By default, the distance metric for % finding nearest neighbors is the Euclidean metric. %% % Find the indices of the training data (|X|) that are within 0.5 cm from % each point in the query data (|Y|). r = 0.5; [Idx,D] = rangesearch(Mdl,Y,r); %% % |Idx| and |D| are five-element cell arrays of vectors. The vector values % in |Idx| are the indices in |X|. The |X| indices represent the % observations that are within 0.5 cm of the query data, |Y|. |D| contains % the distances that correspond to the observations. %% % Display the results for query observation 3. Idx{3} D{3} %% % The closest observation to |Y(3,:)| is |X(127,:)|, which is |0.2646| cm % away. The next closest is |X(122,:)|, which is |0.4359| cm away. All % other observations are greater than |0.5| cm away from |Y(5,:)|.