Get closest coordinate in 2D array
up vote
2
down vote
favorite
coordinates = [(-225.0, -299.5), (-150.0, 75.5), (0.0, 0.0), (225.0, 300.5)]
xy = (-222.4, -204.5)
What is the best way so that a given value xy can be compared to a 2D list of coordinates, and return the index number of the closest coordinate?
In this example, xy would be compared to the coordinates list and thus return the closest coordinate (-225.0, -299.5) or, more ideally, the index number 0.
I've tried researching for a method with itertools or numpy, but couldn't seem to understand how to get the result I want in my example.
python list multidimensional-array coordinates itertools
add a comment |
up vote
2
down vote
favorite
coordinates = [(-225.0, -299.5), (-150.0, 75.5), (0.0, 0.0), (225.0, 300.5)]
xy = (-222.4, -204.5)
What is the best way so that a given value xy can be compared to a 2D list of coordinates, and return the index number of the closest coordinate?
In this example, xy would be compared to the coordinates list and thus return the closest coordinate (-225.0, -299.5) or, more ideally, the index number 0.
I've tried researching for a method with itertools or numpy, but couldn't seem to understand how to get the result I want in my example.
python list multidimensional-array coordinates itertools
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
coordinates = [(-225.0, -299.5), (-150.0, 75.5), (0.0, 0.0), (225.0, 300.5)]
xy = (-222.4, -204.5)
What is the best way so that a given value xy can be compared to a 2D list of coordinates, and return the index number of the closest coordinate?
In this example, xy would be compared to the coordinates list and thus return the closest coordinate (-225.0, -299.5) or, more ideally, the index number 0.
I've tried researching for a method with itertools or numpy, but couldn't seem to understand how to get the result I want in my example.
python list multidimensional-array coordinates itertools
coordinates = [(-225.0, -299.5), (-150.0, 75.5), (0.0, 0.0), (225.0, 300.5)]
xy = (-222.4, -204.5)
What is the best way so that a given value xy can be compared to a 2D list of coordinates, and return the index number of the closest coordinate?
In this example, xy would be compared to the coordinates list and thus return the closest coordinate (-225.0, -299.5) or, more ideally, the index number 0.
I've tried researching for a method with itertools or numpy, but couldn't seem to understand how to get the result I want in my example.
python list multidimensional-array coordinates itertools
python list multidimensional-array coordinates itertools
edited Nov 12 at 8:00
martineau
65.5k988177
65.5k988177
asked Nov 12 at 7:32
qbuffer
416
416
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
up vote
2
down vote
accepted
You can use min
with a proper key
function. Sth along the following lines for instance:
coordinates = [(-225.0, -299.5), (-150.0, 75.5), (0.0, 0.0), (225.0, 300.5)]
xy = (-222.4, -204.5)
dist = lambda x, y: (x[0]-y[0])**2 + (x[1]-y[1])**2
min(coordinates, key=lambda co: dist(co, xy))
# (-225.0, -299.5)
I would suggest usingmath.hypot()
instead—it's probably faster.
– martineau
Nov 12 at 8:03
@martineau I don't quite see how the distance from the origin helps in this general distance case.
– schwobaseggl
Nov 12 at 8:13
min(coordinates, key=lambda p: math.hypot(p[0]-xy[0], p[1]-xy[1]))
– martineau
Nov 12 at 8:44
@martineau So you are adding an extra function call and still doing calculations in pure Python? Not too convincing....
– schwobaseggl
Nov 12 at 8:46
Timing the execution is the only definitive way to tell. Doing so, I found that for larger numbers of points, your way is about 19% slower than usinghypot()
with Python 3.7.1. Here's the benchmark used to determine this (results are at the very end).
– martineau
Nov 12 at 19:19
add a comment |
up vote
1
down vote
Your question is equivalent to : How do I sort a Python list using a custom method to define the sorting key. This can be done in raw python without using external libraries.
When using the sorted()
function of python you can pass a lambda to the key
argument to get the sort done on a specific key.
From there you just have to define the key as your own distance calculation method (here using the distances between the points):
from math import *
coordinates = [(-225.0, -299.5), (-150.0, 75.5), (0.0, 0.0), (225.0, 300.5)]
xy = (-222.4, -204.5)
results = sorted(coordinates, key= lambda v: sqrt(pow((v[0] - xy[0]), 2) + pow((v[1] - xy[1]), 2)))
# Output : [(-225.0, -299.5), (-150.0, 75.5), (0.0, 0.0), (225.0, 300.5)]
From there you can just take the first element of the list if you want the closer point, etc. If you still want to external module I think you can use some third parties function such as from scipy.spatial import distance
as the key parameter of the sort.
Sorting a list with a custom key function involves quite a bit more processing than simply finding the list element that produces the minimum when passed to that functiont—so, no, the question isn't equivalent to that. If it were, I would suggest using themath.hypot()
function because it's faster that the way you're doing it. See this comment about that.
– martineau
Nov 12 at 19:30
add a comment |
up vote
0
down vote
You could simply create a function that iterates through the list of coordinates and keep the index of the one in which the distance between two points is the smallest (using the Pythagorean theorem).
However, if you need something fast given by an external module rather than writing your own, I am not aware of libraries I already used that already have that function, so I'm not helpful here.
I thought about this, but wouldn't the Pythagorean theorem not work if I have negative and positive coordinates in my list? e.g. if coordinates = [(-255.0,-255.0), (255.0,255.0)] -> it wouldn't know which coordinate is closer?
– qbuffer
Nov 12 at 7:46
@Ben There is no issue with negative coordinates, Pythagoras will still take good care of the correct distance. You just have to decide on a tie breaker if in deed, two sets of coordinates havce the same distance.
– schwobaseggl
Nov 12 at 7:50
If you have negative and positive coordinates it will surely work, the formula doesn't care if you have positive or negative coordinates. link If the distance from a specific point from two other points is the same, it will take the index of the first point that appears on the list.
– marcioz98
Nov 12 at 7:55
add a comment |
up vote
0
down vote
Using scipy.spatial.KDTree:
from scipy import spatial
import numpy as np
coordinates = [(-225.0, -299.5), (-150.0, 75.5), (0.0, 0.0), (225.0, 300.5)]
x = [(-222.4, -204.5)]
distance,index = spatial.KDTree(coordinates).query(x)
print(distance)
print(index)
The kd-tree method is O(N*log(N)) and is much faster than Brute Force method that takes O(N**2) time for large enough N.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257607%2fget-closest-coordinate-in-2d-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You can use min
with a proper key
function. Sth along the following lines for instance:
coordinates = [(-225.0, -299.5), (-150.0, 75.5), (0.0, 0.0), (225.0, 300.5)]
xy = (-222.4, -204.5)
dist = lambda x, y: (x[0]-y[0])**2 + (x[1]-y[1])**2
min(coordinates, key=lambda co: dist(co, xy))
# (-225.0, -299.5)
I would suggest usingmath.hypot()
instead—it's probably faster.
– martineau
Nov 12 at 8:03
@martineau I don't quite see how the distance from the origin helps in this general distance case.
– schwobaseggl
Nov 12 at 8:13
min(coordinates, key=lambda p: math.hypot(p[0]-xy[0], p[1]-xy[1]))
– martineau
Nov 12 at 8:44
@martineau So you are adding an extra function call and still doing calculations in pure Python? Not too convincing....
– schwobaseggl
Nov 12 at 8:46
Timing the execution is the only definitive way to tell. Doing so, I found that for larger numbers of points, your way is about 19% slower than usinghypot()
with Python 3.7.1. Here's the benchmark used to determine this (results are at the very end).
– martineau
Nov 12 at 19:19
add a comment |
up vote
2
down vote
accepted
You can use min
with a proper key
function. Sth along the following lines for instance:
coordinates = [(-225.0, -299.5), (-150.0, 75.5), (0.0, 0.0), (225.0, 300.5)]
xy = (-222.4, -204.5)
dist = lambda x, y: (x[0]-y[0])**2 + (x[1]-y[1])**2
min(coordinates, key=lambda co: dist(co, xy))
# (-225.0, -299.5)
I would suggest usingmath.hypot()
instead—it's probably faster.
– martineau
Nov 12 at 8:03
@martineau I don't quite see how the distance from the origin helps in this general distance case.
– schwobaseggl
Nov 12 at 8:13
min(coordinates, key=lambda p: math.hypot(p[0]-xy[0], p[1]-xy[1]))
– martineau
Nov 12 at 8:44
@martineau So you are adding an extra function call and still doing calculations in pure Python? Not too convincing....
– schwobaseggl
Nov 12 at 8:46
Timing the execution is the only definitive way to tell. Doing so, I found that for larger numbers of points, your way is about 19% slower than usinghypot()
with Python 3.7.1. Here's the benchmark used to determine this (results are at the very end).
– martineau
Nov 12 at 19:19
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You can use min
with a proper key
function. Sth along the following lines for instance:
coordinates = [(-225.0, -299.5), (-150.0, 75.5), (0.0, 0.0), (225.0, 300.5)]
xy = (-222.4, -204.5)
dist = lambda x, y: (x[0]-y[0])**2 + (x[1]-y[1])**2
min(coordinates, key=lambda co: dist(co, xy))
# (-225.0, -299.5)
You can use min
with a proper key
function. Sth along the following lines for instance:
coordinates = [(-225.0, -299.5), (-150.0, 75.5), (0.0, 0.0), (225.0, 300.5)]
xy = (-222.4, -204.5)
dist = lambda x, y: (x[0]-y[0])**2 + (x[1]-y[1])**2
min(coordinates, key=lambda co: dist(co, xy))
# (-225.0, -299.5)
answered Nov 12 at 7:46
schwobaseggl
35.9k32139
35.9k32139
I would suggest usingmath.hypot()
instead—it's probably faster.
– martineau
Nov 12 at 8:03
@martineau I don't quite see how the distance from the origin helps in this general distance case.
– schwobaseggl
Nov 12 at 8:13
min(coordinates, key=lambda p: math.hypot(p[0]-xy[0], p[1]-xy[1]))
– martineau
Nov 12 at 8:44
@martineau So you are adding an extra function call and still doing calculations in pure Python? Not too convincing....
– schwobaseggl
Nov 12 at 8:46
Timing the execution is the only definitive way to tell. Doing so, I found that for larger numbers of points, your way is about 19% slower than usinghypot()
with Python 3.7.1. Here's the benchmark used to determine this (results are at the very end).
– martineau
Nov 12 at 19:19
add a comment |
I would suggest usingmath.hypot()
instead—it's probably faster.
– martineau
Nov 12 at 8:03
@martineau I don't quite see how the distance from the origin helps in this general distance case.
– schwobaseggl
Nov 12 at 8:13
min(coordinates, key=lambda p: math.hypot(p[0]-xy[0], p[1]-xy[1]))
– martineau
Nov 12 at 8:44
@martineau So you are adding an extra function call and still doing calculations in pure Python? Not too convincing....
– schwobaseggl
Nov 12 at 8:46
Timing the execution is the only definitive way to tell. Doing so, I found that for larger numbers of points, your way is about 19% slower than usinghypot()
with Python 3.7.1. Here's the benchmark used to determine this (results are at the very end).
– martineau
Nov 12 at 19:19
I would suggest using
math.hypot()
instead—it's probably faster.– martineau
Nov 12 at 8:03
I would suggest using
math.hypot()
instead—it's probably faster.– martineau
Nov 12 at 8:03
@martineau I don't quite see how the distance from the origin helps in this general distance case.
– schwobaseggl
Nov 12 at 8:13
@martineau I don't quite see how the distance from the origin helps in this general distance case.
– schwobaseggl
Nov 12 at 8:13
min(coordinates, key=lambda p: math.hypot(p[0]-xy[0], p[1]-xy[1]))
– martineau
Nov 12 at 8:44
min(coordinates, key=lambda p: math.hypot(p[0]-xy[0], p[1]-xy[1]))
– martineau
Nov 12 at 8:44
@martineau So you are adding an extra function call and still doing calculations in pure Python? Not too convincing....
– schwobaseggl
Nov 12 at 8:46
@martineau So you are adding an extra function call and still doing calculations in pure Python? Not too convincing....
– schwobaseggl
Nov 12 at 8:46
Timing the execution is the only definitive way to tell. Doing so, I found that for larger numbers of points, your way is about 19% slower than using
hypot()
with Python 3.7.1. Here's the benchmark used to determine this (results are at the very end).– martineau
Nov 12 at 19:19
Timing the execution is the only definitive way to tell. Doing so, I found that for larger numbers of points, your way is about 19% slower than using
hypot()
with Python 3.7.1. Here's the benchmark used to determine this (results are at the very end).– martineau
Nov 12 at 19:19
add a comment |
up vote
1
down vote
Your question is equivalent to : How do I sort a Python list using a custom method to define the sorting key. This can be done in raw python without using external libraries.
When using the sorted()
function of python you can pass a lambda to the key
argument to get the sort done on a specific key.
From there you just have to define the key as your own distance calculation method (here using the distances between the points):
from math import *
coordinates = [(-225.0, -299.5), (-150.0, 75.5), (0.0, 0.0), (225.0, 300.5)]
xy = (-222.4, -204.5)
results = sorted(coordinates, key= lambda v: sqrt(pow((v[0] - xy[0]), 2) + pow((v[1] - xy[1]), 2)))
# Output : [(-225.0, -299.5), (-150.0, 75.5), (0.0, 0.0), (225.0, 300.5)]
From there you can just take the first element of the list if you want the closer point, etc. If you still want to external module I think you can use some third parties function such as from scipy.spatial import distance
as the key parameter of the sort.
Sorting a list with a custom key function involves quite a bit more processing than simply finding the list element that produces the minimum when passed to that functiont—so, no, the question isn't equivalent to that. If it were, I would suggest using themath.hypot()
function because it's faster that the way you're doing it. See this comment about that.
– martineau
Nov 12 at 19:30
add a comment |
up vote
1
down vote
Your question is equivalent to : How do I sort a Python list using a custom method to define the sorting key. This can be done in raw python without using external libraries.
When using the sorted()
function of python you can pass a lambda to the key
argument to get the sort done on a specific key.
From there you just have to define the key as your own distance calculation method (here using the distances between the points):
from math import *
coordinates = [(-225.0, -299.5), (-150.0, 75.5), (0.0, 0.0), (225.0, 300.5)]
xy = (-222.4, -204.5)
results = sorted(coordinates, key= lambda v: sqrt(pow((v[0] - xy[0]), 2) + pow((v[1] - xy[1]), 2)))
# Output : [(-225.0, -299.5), (-150.0, 75.5), (0.0, 0.0), (225.0, 300.5)]
From there you can just take the first element of the list if you want the closer point, etc. If you still want to external module I think you can use some third parties function such as from scipy.spatial import distance
as the key parameter of the sort.
Sorting a list with a custom key function involves quite a bit more processing than simply finding the list element that produces the minimum when passed to that functiont—so, no, the question isn't equivalent to that. If it were, I would suggest using themath.hypot()
function because it's faster that the way you're doing it. See this comment about that.
– martineau
Nov 12 at 19:30
add a comment |
up vote
1
down vote
up vote
1
down vote
Your question is equivalent to : How do I sort a Python list using a custom method to define the sorting key. This can be done in raw python without using external libraries.
When using the sorted()
function of python you can pass a lambda to the key
argument to get the sort done on a specific key.
From there you just have to define the key as your own distance calculation method (here using the distances between the points):
from math import *
coordinates = [(-225.0, -299.5), (-150.0, 75.5), (0.0, 0.0), (225.0, 300.5)]
xy = (-222.4, -204.5)
results = sorted(coordinates, key= lambda v: sqrt(pow((v[0] - xy[0]), 2) + pow((v[1] - xy[1]), 2)))
# Output : [(-225.0, -299.5), (-150.0, 75.5), (0.0, 0.0), (225.0, 300.5)]
From there you can just take the first element of the list if you want the closer point, etc. If you still want to external module I think you can use some third parties function such as from scipy.spatial import distance
as the key parameter of the sort.
Your question is equivalent to : How do I sort a Python list using a custom method to define the sorting key. This can be done in raw python without using external libraries.
When using the sorted()
function of python you can pass a lambda to the key
argument to get the sort done on a specific key.
From there you just have to define the key as your own distance calculation method (here using the distances between the points):
from math import *
coordinates = [(-225.0, -299.5), (-150.0, 75.5), (0.0, 0.0), (225.0, 300.5)]
xy = (-222.4, -204.5)
results = sorted(coordinates, key= lambda v: sqrt(pow((v[0] - xy[0]), 2) + pow((v[1] - xy[1]), 2)))
# Output : [(-225.0, -299.5), (-150.0, 75.5), (0.0, 0.0), (225.0, 300.5)]
From there you can just take the first element of the list if you want the closer point, etc. If you still want to external module I think you can use some third parties function such as from scipy.spatial import distance
as the key parameter of the sort.
answered Nov 12 at 7:49
Anto
1062
1062
Sorting a list with a custom key function involves quite a bit more processing than simply finding the list element that produces the minimum when passed to that functiont—so, no, the question isn't equivalent to that. If it were, I would suggest using themath.hypot()
function because it's faster that the way you're doing it. See this comment about that.
– martineau
Nov 12 at 19:30
add a comment |
Sorting a list with a custom key function involves quite a bit more processing than simply finding the list element that produces the minimum when passed to that functiont—so, no, the question isn't equivalent to that. If it were, I would suggest using themath.hypot()
function because it's faster that the way you're doing it. See this comment about that.
– martineau
Nov 12 at 19:30
Sorting a list with a custom key function involves quite a bit more processing than simply finding the list element that produces the minimum when passed to that functiont—so, no, the question isn't equivalent to that. If it were, I would suggest using the
math.hypot()
function because it's faster that the way you're doing it. See this comment about that.– martineau
Nov 12 at 19:30
Sorting a list with a custom key function involves quite a bit more processing than simply finding the list element that produces the minimum when passed to that functiont—so, no, the question isn't equivalent to that. If it were, I would suggest using the
math.hypot()
function because it's faster that the way you're doing it. See this comment about that.– martineau
Nov 12 at 19:30
add a comment |
up vote
0
down vote
You could simply create a function that iterates through the list of coordinates and keep the index of the one in which the distance between two points is the smallest (using the Pythagorean theorem).
However, if you need something fast given by an external module rather than writing your own, I am not aware of libraries I already used that already have that function, so I'm not helpful here.
I thought about this, but wouldn't the Pythagorean theorem not work if I have negative and positive coordinates in my list? e.g. if coordinates = [(-255.0,-255.0), (255.0,255.0)] -> it wouldn't know which coordinate is closer?
– qbuffer
Nov 12 at 7:46
@Ben There is no issue with negative coordinates, Pythagoras will still take good care of the correct distance. You just have to decide on a tie breaker if in deed, two sets of coordinates havce the same distance.
– schwobaseggl
Nov 12 at 7:50
If you have negative and positive coordinates it will surely work, the formula doesn't care if you have positive or negative coordinates. link If the distance from a specific point from two other points is the same, it will take the index of the first point that appears on the list.
– marcioz98
Nov 12 at 7:55
add a comment |
up vote
0
down vote
You could simply create a function that iterates through the list of coordinates and keep the index of the one in which the distance between two points is the smallest (using the Pythagorean theorem).
However, if you need something fast given by an external module rather than writing your own, I am not aware of libraries I already used that already have that function, so I'm not helpful here.
I thought about this, but wouldn't the Pythagorean theorem not work if I have negative and positive coordinates in my list? e.g. if coordinates = [(-255.0,-255.0), (255.0,255.0)] -> it wouldn't know which coordinate is closer?
– qbuffer
Nov 12 at 7:46
@Ben There is no issue with negative coordinates, Pythagoras will still take good care of the correct distance. You just have to decide on a tie breaker if in deed, two sets of coordinates havce the same distance.
– schwobaseggl
Nov 12 at 7:50
If you have negative and positive coordinates it will surely work, the formula doesn't care if you have positive or negative coordinates. link If the distance from a specific point from two other points is the same, it will take the index of the first point that appears on the list.
– marcioz98
Nov 12 at 7:55
add a comment |
up vote
0
down vote
up vote
0
down vote
You could simply create a function that iterates through the list of coordinates and keep the index of the one in which the distance between two points is the smallest (using the Pythagorean theorem).
However, if you need something fast given by an external module rather than writing your own, I am not aware of libraries I already used that already have that function, so I'm not helpful here.
You could simply create a function that iterates through the list of coordinates and keep the index of the one in which the distance between two points is the smallest (using the Pythagorean theorem).
However, if you need something fast given by an external module rather than writing your own, I am not aware of libraries I already used that already have that function, so I'm not helpful here.
answered Nov 12 at 7:40
marcioz98
1
1
I thought about this, but wouldn't the Pythagorean theorem not work if I have negative and positive coordinates in my list? e.g. if coordinates = [(-255.0,-255.0), (255.0,255.0)] -> it wouldn't know which coordinate is closer?
– qbuffer
Nov 12 at 7:46
@Ben There is no issue with negative coordinates, Pythagoras will still take good care of the correct distance. You just have to decide on a tie breaker if in deed, two sets of coordinates havce the same distance.
– schwobaseggl
Nov 12 at 7:50
If you have negative and positive coordinates it will surely work, the formula doesn't care if you have positive or negative coordinates. link If the distance from a specific point from two other points is the same, it will take the index of the first point that appears on the list.
– marcioz98
Nov 12 at 7:55
add a comment |
I thought about this, but wouldn't the Pythagorean theorem not work if I have negative and positive coordinates in my list? e.g. if coordinates = [(-255.0,-255.0), (255.0,255.0)] -> it wouldn't know which coordinate is closer?
– qbuffer
Nov 12 at 7:46
@Ben There is no issue with negative coordinates, Pythagoras will still take good care of the correct distance. You just have to decide on a tie breaker if in deed, two sets of coordinates havce the same distance.
– schwobaseggl
Nov 12 at 7:50
If you have negative and positive coordinates it will surely work, the formula doesn't care if you have positive or negative coordinates. link If the distance from a specific point from two other points is the same, it will take the index of the first point that appears on the list.
– marcioz98
Nov 12 at 7:55
I thought about this, but wouldn't the Pythagorean theorem not work if I have negative and positive coordinates in my list? e.g. if coordinates = [(-255.0,-255.0), (255.0,255.0)] -> it wouldn't know which coordinate is closer?
– qbuffer
Nov 12 at 7:46
I thought about this, but wouldn't the Pythagorean theorem not work if I have negative and positive coordinates in my list? e.g. if coordinates = [(-255.0,-255.0), (255.0,255.0)] -> it wouldn't know which coordinate is closer?
– qbuffer
Nov 12 at 7:46
@Ben There is no issue with negative coordinates, Pythagoras will still take good care of the correct distance. You just have to decide on a tie breaker if in deed, two sets of coordinates havce the same distance.
– schwobaseggl
Nov 12 at 7:50
@Ben There is no issue with negative coordinates, Pythagoras will still take good care of the correct distance. You just have to decide on a tie breaker if in deed, two sets of coordinates havce the same distance.
– schwobaseggl
Nov 12 at 7:50
If you have negative and positive coordinates it will surely work, the formula doesn't care if you have positive or negative coordinates. link If the distance from a specific point from two other points is the same, it will take the index of the first point that appears on the list.
– marcioz98
Nov 12 at 7:55
If you have negative and positive coordinates it will surely work, the formula doesn't care if you have positive or negative coordinates. link If the distance from a specific point from two other points is the same, it will take the index of the first point that appears on the list.
– marcioz98
Nov 12 at 7:55
add a comment |
up vote
0
down vote
Using scipy.spatial.KDTree:
from scipy import spatial
import numpy as np
coordinates = [(-225.0, -299.5), (-150.0, 75.5), (0.0, 0.0), (225.0, 300.5)]
x = [(-222.4, -204.5)]
distance,index = spatial.KDTree(coordinates).query(x)
print(distance)
print(index)
The kd-tree method is O(N*log(N)) and is much faster than Brute Force method that takes O(N**2) time for large enough N.
add a comment |
up vote
0
down vote
Using scipy.spatial.KDTree:
from scipy import spatial
import numpy as np
coordinates = [(-225.0, -299.5), (-150.0, 75.5), (0.0, 0.0), (225.0, 300.5)]
x = [(-222.4, -204.5)]
distance,index = spatial.KDTree(coordinates).query(x)
print(distance)
print(index)
The kd-tree method is O(N*log(N)) and is much faster than Brute Force method that takes O(N**2) time for large enough N.
add a comment |
up vote
0
down vote
up vote
0
down vote
Using scipy.spatial.KDTree:
from scipy import spatial
import numpy as np
coordinates = [(-225.0, -299.5), (-150.0, 75.5), (0.0, 0.0), (225.0, 300.5)]
x = [(-222.4, -204.5)]
distance,index = spatial.KDTree(coordinates).query(x)
print(distance)
print(index)
The kd-tree method is O(N*log(N)) and is much faster than Brute Force method that takes O(N**2) time for large enough N.
Using scipy.spatial.KDTree:
from scipy import spatial
import numpy as np
coordinates = [(-225.0, -299.5), (-150.0, 75.5), (0.0, 0.0), (225.0, 300.5)]
x = [(-222.4, -204.5)]
distance,index = spatial.KDTree(coordinates).query(x)
print(distance)
print(index)
The kd-tree method is O(N*log(N)) and is much faster than Brute Force method that takes O(N**2) time for large enough N.
answered Nov 12 at 8:10
Abhinav Vajpeyi
163
163
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257607%2fget-closest-coordinate-in-2d-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown