Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get nearest coordinate in a 2D numpy array

I’ve seen many of the posts on how to get closes value in a numpy array, how to get closest coordinate in a 2D array etc. But none of them seem to solve what I am looking for.

The problem is, I have a 2D numpy array as such:

[[77.62881735 12.91172607]          
 [77.6464534  12.9230648]
 [77.65330961 12.92020244]
 [77.63142413 12.90909731]]

And I have one numpy array like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[77.64000112 12.91602265]

Now I want to find a coordinate in the 2D numpy array that is closest to the co-ordinates in 1D array.

That said, I am a beginner in these stuffs..So any input is appreciated.

>Solution :

I assume you mean euclidean distance. Try this:

a = np.array([[77.62881735, 12.91172607],          
              [77.6464534,  12.9230648],
 [77.65330961,12.92020244],
 [77.63142413 ,12.90909731]])

b = np.array([77.64000112, 12.91602265])

idx_min = np.sum( (a-b)**2, axis=1, keepdims=True).argmin(axis=0)
idx_min, a[idx_min]

Output:

(array([1], dtype=int64), array([[77.6464534, 12.9230648]]))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading