# TWODSORT is the jMap module of jLab.

``` TWODSORT  Distances from data points to nearby grid points.

[DS,XS,YS]=TWODSORT(X,Y,XO,YO,CUTOFF) returns sorted distances D
between data points at locations X,Y and grid points at XO,YO.

X and Y are arrays of the same size into data point locations. XO and
YO are arrays of length M and N, say, specifying the bin center
locations of an M x N matrix of grid points, i.e.

XO= [XO_1 XO_2 ... XO_N]      XO =  [YO_1;
YO_2;
...
YO_M]

CUTOFF is the maximum distance to be included in the output arrays.

The output arrays are M numerical arrays arranged as a length M cell
array.  That is, there is one cell per element of Y0. Each numerical
array has N columns, i.e., the number of elements of X0, with the
number of rows varying between arrays.

DS gives the distances SQRT((X-XO)^2+(Y-YO)^2) of all data points less
than the CUTOFF distance from the (m,n)th grid point, sorted in order
of increasing distance.  Entries farther than CUTOFF in all output
fields are filled with NaNs.

XS and YS are corresponding deviations X-XO and Y-YO from the grid
point location to each data point.

The choice to put rows into cell arrays is made because for consistency
with SPHERESORT and for convenience in parallelizing POLYSMOOTH.
_________________________________________________________________

Limiting output dimension

[DS,XS,YS]=TWODSORT(X,Y,XO,YO,[CUTOFF JMAX]), where the fifth input
argument is a 2-vector, additionally specifies that number of rows of
in each cell of the output will be no larger than JMAX.  This option is
useful for the 'fixed population' algorithm in POLYSMOOTH.
_________________________________________________________________

Let's say some additional variables Z1, Z2,...,ZK are given at the data
locations X,Y.  Then

[DS,XS,YS,Z1S,Z2S,...,ZKS]=

TWODSORT(X,Y,Z1,Z2,...,ZK,XO,YO,CUTOFF);

also returns the values of these variables.

Z1S, Z2S,...,ZKS are the same size as the other output arguments, and
give the values of Z1, Z2,...,ZK sorted according to distance.

When there are multiple fields to be mapped, one may instead wish to
use the approach described under "One grid, many fields" in POLYSMOOTH.
_________________________________________________________________