function nearest = find_closest ( m, n, x, generator ) %% FIND_CLOSEST finds the Voronoi cell generator closest to a point X. % % Discussion: % % This routine finds the closest Voronoi cell generator by checking every % one. For problems with many cells, this process can take the bulk % of the CPU time. Other approaches, which group the cell generators into % bins, can run faster by a large factor. % % Modified: % % 27 April 2003 % % Parameters: % % Input, integer M, the spatial dimension. % % Input, integer N, the number of cell generatorrs. % % Input, real X(M), the point to be checked. % % Input, real GENERATOR(M,N), the cell generators. % % Output, integer NEAREST, the index of the nearest cell generator. % nearest = 0; distance = Inf; for j = 1 : n dist_sq = 0.0; for i = 1 : m dist_sq = dist_sq + ( generator(i,j) - x(i) )^2; end if ( dist_sq < distance ) distance = dist_sq; nearest = j; end end distance = sqrt ( distance );