MATLAB Examples

# near2 documentation

This function returns indices of values in X and Y that are close to some x,y point. It's similar to find, for nearest neighbors, on a 2D grid.

## Syntax

```ind = near2(X,Y,xi,yi)
[rowi,coli] = near2(X,Y,xi,yi)```

## Description

ind = near2(X,Y,xi,yi) returns a linear index corresponding to values in X and Y that are closest to the point given by ( xi, yi ). X and Y must be 2D grids of equal size and xi and yi must be scalar.

[rowi,coli] = near2(X,Y,xi,yi) returns the row and column indices

## Examples

First create a grid of X and Y data:

```[X,Y] = meshgrid(1:5,10:-1:5) ```
```X = 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 Y = 10 10 10 10 10 9 9 9 9 9 8 8 8 8 8 7 7 7 7 7 6 6 6 6 6 5 5 5 5 5 ```

Now we can look for X, Y indices corresponding to the point closest to (2.1,9.724):

```[rowi,coli] = near2(X,Y,2.1,9.724) ```
```rowi = 1 coli = 2 ```

Or if you'd prefer linear indices,

```ind = near2(X,Y,2.1,9.724) ```
```ind = 7 ```

If xi,yi describes some point equidistant between multiple grid points in X and Y, all valid minimum-distance indices are returned:

```[rowi,coli] = near2(X,Y,2.5,9.1) ```
```rowi = 2 2 coli = 2 3 ```

## Author Info

This function was written by Chad A. Greene of the University of Texas Institute for Geophysics (UTIG) in October 2014.