MATLAB: Finding Neighbors of cells in a voronoi diagram

voronoi

I used the built in matlab function to create a voronoi diagram, and am now trying to find the neighbors of each cell, using the vertices and matlab built in function nearestneighbors, but I am having trouble understanding the inputs and what the vertices output actually is (I get two columns). Any help would be great! Thanks!

Best Answer

N = length(c) ; % total number of v/c
IDc = cell(N,1) ; % Initialize to store neighbors of every c
nn = 5 ; % number of neighbors wanted
for i1 = 1:N % loop for each c
Ni = length(c{i1}) ; % number of points in every c
IDci = cell(Ni,1) ; % initialization to store each c
for j1 = 1:Ni % loop for every point in c
[IDX,D] = knnsearch(Pos,v(c{i1}(j1),:),'k',nn) ;
IDci{j1} = IDX ; % store the neighbors
end
IDc{i1} = IDci ;
end