# Find matrix location of the nearest point?

**URL:** <https://community.wolfram.com/t/find-matrix-location-of-the-nearest-point/15067>\
**Category:** Questions\
**Tags:** optimization\
**Created:** [November 30, 2019, 7:54pm UTC](https://community.wolfram.com/t/find-matrix-location-of-the-nearest-point/15067 "2019-11-30T19:54:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiaxingxiong](https://avatars.discourse-cdn.com/v4/letter/x/3ec8ea/32.png) [@xiaxingxiong](https://community.wolfram.com/u/xiaxingxiong)\
**Post date:** [November 30, 2019, 7:54pm UTC](https://community.wolfram.com/t/find-matrix-location-of-the-nearest-point/15067/1 "2019-11-30T19:54:57Z")

</div>

1. How to find the matrix location of the nearest point?

2)if I have a original set of points

```
ospts={{2,1},{2,3},{3,4},{4,5},{5,6}.......{4,9}}, 

```

and a target set of points tspts={{1,2}…{3,5}}. For each target point I like to find the nearest original point and its position within the ospts. How should I do it?

Thanks.

Xiaxing

---

<div class="post-metadata">

**Author:** ![rohitnamjoshi](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rohitnamjoshi](https://community.wolfram.com/u/rohitnamjoshi)\
**Post date:** [November 30, 2019, 9:45pm UTC](https://community.wolfram.com/t/find-matrix-location-of-the-nearest-point/15067/2 "2019-11-30T21:45:48Z")

</div>

Hi Xiaxing,

Here is one way to do it

```
SeedRandom[123];
ospts = RandomInteger[25, {10, 2}];
tspts = RandomInteger[25, {10, 2}];
closest = tspts // Map[First@Nearest[ospts, #] &];

ListPlot[{ospts, tspts},
 PlotLegends -> {"ospts", "tspts"},
 Epilog -> {Red, Transpose[{closest, tspts}] // Map[Line]},
 ImageSize -> Large]

```

 ![1374plot.png](https://global.discourse-cdn.com/wolfram/original/2X/0/075cd766af1e1444baa58a332dc15aadf0fe6325.png)

Positions of the closest points

```
positions = closest // Map[Position[ospts, #] &] // Flatten
(* {1, 8, 1, 7, 1, 7, 2, 4, 4, 9} *)

```

---

<div class="post-metadata">

**Author:** ![anon\_91240867](https://avatars.discourse-cdn.com/v4/letter/a/8baadc/32.png) [@anon\_91240867](https://community.wolfram.com/u/anon_91240867)\
**Post date:** [December 2, 2019, 11:14pm UTC](https://community.wolfram.com/t/find-matrix-location-of-the-nearest-point/15067/3 "2019-12-02T23:14:17Z")

</div>

Mathematica’s Help for Nearest shows examples of nearly the same thing. I’m unsure why you are asking how to find the nearest.

If you want the nearest to a set of points you could say “at the center of a set” or “on the boundary of a set”.

but you appear to be asking for merely the nearest of each of one to the other which can be done by List[] operation.

Apply[(Nearest[ opt, #])&, tpt]

a statistical approach might also be interesting

---

<div class="post-metadata">

**Author:** ![rohitnamjoshi](https://avatars.discourse-cdn.com/v4/letter/r/f1d935/32.png) [@rohitnamjoshi](https://community.wolfram.com/u/rohitnamjoshi)\
**Post date:** [December 3, 2019, 12:50am UTC](https://community.wolfram.com/t/find-matrix-location-of-the-nearest-point/15067/4 "2019-12-03T00:50:22Z")

</div>

Hi John,

What do you mean by

> List[] operation

Your proposed solution

```
Apply[(Nearest[opt, #])&, tpt]

```

will just return a single point. How does it answer the first question or the second question?
