## Documentation Center |

On this page… |
---|

Nearest-Neighbor Interpolation Using a delaunayTriangulation Query |

This example shows how to perform nearest-neighbor interpolation on a scattered set of points using a specific Delaunay triangulation.

Create a `delaunayTriangulation` of a
set of scattered points in 2-D.

```
P = -2.5 + 5*gallery('uniformdata',[50 2],0);
DT = delaunayTriangulation(P)
```

DT = delaunayTriangulation with properties: Points: [50x2 double] ConnectivityList: [87x3 double] Constraints: []

Sample a parabolic function, *V*(*x*, *y*),
at the points specified in `P`.

V = P(:,1).^2 + P(:,2).^2;

Define 10 random query points.

```
Pq = -2 + 4*gallery('uniformdata',[10 2],1);
```

Perform nearest-neighbor interpolation on `V` using
the triangulation, `DT`. Use `nearestNeighbor` to
find the indices of the nearest-neighbor vertices, `vi`,
for the set of query points, `Pq`. Then examine the
specific values of `V` at the indices.

vi = nearestNeighbor(DT,Pq); Vq = V(vi)

Vq = 5.3163 0.3453 4.3026 1.2579 1.9435 5.9194 3.9030 0.4172 11.7282 0.8641

This example shows how to perform linear interpolation on a scattered set of points with a specific Delaunay triangulation.

You can use the `delaunayTriangulation` method, `pointLocation`,
to compute the enclosing triangle of a query point and the magnitudes
of the vertex weights. The weights are called barycentric coordinates,
and they represent a partition of unity. That is, the sum of the three
weights equals 1. The interpolated value of a function, *V*,
at a query point is the sum of the weighted values of *V* at
the three vertices. That is, if the function has values, V1, V2, V3
at the three vertices, and the weights are B1, B2, B3, then the interpolated
value is (V1)(B1) + (V2)(B2) + (V3)(B3).

Create a `delaunayTriangulation` of a
set of scattered points in 2-D.

```
P = -2.5 + 5*gallery('uniformdata',[50 2],0);
DT = delaunayTriangulation(P)
```

DT = delaunayTriangulation with properties: Points: [50x2 double] ConnectivityList: [87x3 double] Constraints: []

Sample a parabolic function, *V*(*x*, *y*),
at the points in `P`.

V = P(:,1).^2 + P(:,2).^2;

Define 10 random query points.

```
Pq = -2 + 4*gallery('uniformdata',[10 2],1);
```

Find the triangle that encloses each query point using
the `pointLocation` method. In the code below, `ti` contains
the IDs of the enclosing triangles and `bc` contains
the barycentric coordinates associated with each triangle.

[ti,bc] = pointLocation(DT,Pq);

Find the values of *V*(*x*, *y*)
at the vertices of each enclosing triangle.

triVals = V(DT(ti,:));

Calculate the sum of the weighted values of *V*(*x*, *y*)
using the dot product.

Vq = dot(bc',triVals')'

Vq = 5.9456 1.1222 4.7963 0.9373 2.3533 3.4219 2.3104 0.7728 8.0479 1.0886

`delaunayTriangulation` | `nearestNeighbor` | `pointLocation`

Was this topic helpful?