1. 程式人生 > >基於KD-Tree的最近鄰搜尋

基於KD-Tree的最近鄰搜尋

目標:查詢目標點附近的10個最近鄰鄰居。

load fisheriris  
x = meas(:,3:4);  
figure(100);
g1=gscatter(x(:,1),x(:,2),species);  %species分類中是有三個分類:setosa,versicolor,virginica
legend('Location','best')  
newpoint = [5 1.45];  
line(newpoint(1),newpoint(2),'marker','x','color','k',...  
   'markersize',10,'linewidth',2)  
Mdl 
= KDTreeSearcher(x) ; [n,d] = knnsearch(Mdl,newpoint,'k',10); %k代表個數,即搜尋給定點附近的幾個最近鄰點 line(x(n,1),x(n,2),'color',[.5 .5 .5],'marker','o',... 'linestyle','none','markersize',10) disp(x(n,:))