nearest_neighbor, an Octave code which is given two sets of M-dimensional points R and S; For each point in S, it finds the closest point in R. The code is by Richard Brown.
In a nearest neighbor calculation, we are given:
Obviously, one method to determine the values in NEAREST is simply to compute every distance and take the index of the minimum. But even this simple idea can be implemented in many ways in MATLAB, and implementations will vary in their cost in memory and time.
Also, note that if the dimension M is small, and if the size of the R set is small relative to that of S, it may be much cheaper to compute the Delaunay triangulation of R (or its higher-dimensional generalization). Computing the triangulation is somewhat expensive, but makes the search procedure extremely quick.
Richard Brown's function tries to use MATLAB's Delaunay search algorithm when it seems preferable, and otherwise computes the nearest neighbor by the straightforward approach.
Copyright (c) 2009, Richard Brown. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted if accompanied by the following disclaimer:
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
nearest_neighbor is available in a MATLAB version and an Octave version.
closest_point_brute, an Octave code which tests the time complexity of various procedures for solving the nearest neighbor problem.
cvt, an Octave code which computes elements of a Centroidal Voronoi Tessellation (CVT).
nearest_interp_1d, an Octave code which interpolates a set of data using a piecewise constant interpolant defined by the nearest neighbor criterion.