## ./

# nearestn.pro

nearestN takes takes a list of source locations and, for a list of test positions, returns the index of the n'th nearest source.

## Routines

`result = nearestn_findNeighbors(x, y, n, h, ri, nx, ny)`

findNeighbors is an internal routine used by nearestN.

`result = nearestn(points, catalog, n [, dist=dist] [, all=all] [, verbose=verbose])`

This function returns the nth closest object to a given set of coordinates

## Routine details

## topnearestn_findNeighbors

`result = nearestn_findNeighbors(x, y, n, h, ri, nx, ny)`

findNeighbors is an internal routine used by nearestN. Given an (x,y) point and histogram information about a set of sample points, this function returns a list of at least n objects, one of which is guaranteed to be the nth nearest neighbor.

The function uses the reverse_indices variable from histogram to effeciently index objects nearby (x,y).

### Return value

A list of indices referenceing objects in the histogram created in nearestN. One of these objects is guaranteed to be the nth nearest neighbor.

### Parameters

- x in required
The x data point to find neighbors for

- y in required
The y data point to find neighbors for

- n in required
Ensure that the n'th nearest neighbor is included in the output

- h in required
The histgogram created in nearestN

- ri in required
The reverse indices variable from the histogram calculated in nearestN

- nx in required
The number of bins along the x axis in the histogram described by h

- ny in required
The number of bins along the y axis in the histogram described by h

## topnearestn

catalog processing

`result = nearestn(points, catalog, n [, dist=dist] [, all=all] [, verbose=verbose])`

This function returns the nth closest object to a given set of coordinates

Designed to scale roughly linearly with the catalog size, as opposed to more common O(n^2) methods (e.g. computing every distance for each point and sorting).

Coordinate space is assumed to be euclidian.

### Return value

A vector whose ith row gives the index of the nth closest point to points[i]

### Parameters

- points in required
The points to find the nth nearest neighbor for

- catalog in required
A (2,m) array of m (x,y) points

- n in required
Find the n'th closest neighbor. The closest point corresponds to n = 0

### Keywords

- dist in optional
A variable to hold the distances between the points and their nth nearest neighbors.

- all in optional
If set, returns all n+1 closest neightbor indices. In this case, the result is a n + 1 by m array

- verbose in optional
Print extra output

### Author information

- History:
Written by: Chris Beaumont, September 2008 December 2008: Updated so that the points to find neighbors for need not be the list of object coordinates itself. cnb. March 2009: Added the DIST keyword. cnb. March 2009: Fixed bug when requesting n = 0. Added stricter input checking. cnb. March 2009: Added ALL keywrod. cnb. April 2009: Fixed bug in which test points outside the cloud of catalog points caused a crash. cnb. May 2009: Added VERBOSE keyword. cnb. June 2009: Reworked VERBOSE outputs to be used with VERBIAGE program. cnb. November 2009: Fixed bug that incorrectly populated the DIST array when /ALL was set

## File attributes

Modifcation date: | Mon Mar 22 16:17:13 2010 |

Lines: | 211 |