3

I am writing some code to calculate the real distance between one point and the rest of the points from the same array. The array holds positions of particles in 3D space. There is N-particles so the array's shape is (N,3). I choose one particle and calculate the distance between this particle and the rest of the particles, all within one array.

Would anyone here have any idea how to do this?

What I have so far:

xbox = 10
ybox = 10
zbox = 10
nparticles =15
positions = np.empty([nparticles, 3])
for i in range(nparticles):
     xrandomalocation = random.uniform(0, xbox)
     yrandomalocation = random.uniform(0, ybox)
     zrandomalocation = random.uniform(0, zbox)
     positions[i, 0] = xrandomalocation
     positions[i, 1] = yrandomalocation
     positions[i, 2] = zrandomalocation

And that's pretty much all I have right now. I was thinking of using np.linalg.norm however I am not sure at all how to implement it to my code (or maybe use it in a loop)?

6
  • 2
    can you put some code which you've tried ? atleast the array ? Commented Feb 4, 2018 at 15:37
  • Does this (first line in the question) help? Commented Feb 4, 2018 at 15:37
  • Welcome to StackOverflow! Please read about how to ask a question (particularly how to create a good example) in order to get good responses. In your case posting a sample array and expected output would be useful. Commented Feb 4, 2018 at 15:39
  • 2
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the FAQ and How to Ask. Commented Feb 4, 2018 at 15:40
  • @user202729 I have added what I have now. Commented Feb 4, 2018 at 15:53

2 Answers 2

6

It sounds like you could use scipy.distance.cdist or scipy.distance.pdist for this. For example, to get the distances from point X to the points in coords:

>>> from scipy.spatial import distance
>>> X = [(35.0456, -85.2672)]
>>> coords = [(35.1174, -89.9711),
...           (35.9728, -83.9422),
...           (36.1667, -86.7833)]
>>> distance.cdist(X, coords, 'euclidean')
array([[ 4.70444794,  1.6171966 ,  1.88558331]])

pdist is similar, but only takes one array, and you get the distances between all pairs.

Sign up to request clarification or add additional context in comments.

Comments

0

i am using this function:

from scipy.spatial import distance

def closest_node(node, nodes):
    closest = distance.cdist([node], nodes)
    index = closest.argmin()
    euclidean = closest[0]
    return nodes[index], euclidean[index]

where node is the single point in the space you want to compare with an array of points called nodes. it returns the point and the euclidean distance to your original node

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.