1

So I have two arrays that have x, y, z coordinates. I'm just trying to apply the 3D distance formula. Problem is, that I can't find a post that constitutes arrays with multiple values in each column and spits out an array.

print MW_FirstsubPos1 

[[  51618.7265625   106197.7578125    69647.6484375 ]
 [  33864.1953125    11757.29882812   11849.90332031]
 [  12750.09863281   58954.91015625   38067.0859375 ]
 ..., 
 [  99002.6640625    96021.0546875    18798.44726562]
 [  27180.83984375   74350.421875     78075.78125   ]
 [  19297.88476562   82161.140625      1204.53503418]]

print MW_SecondsubPos1 

[[  51850.9140625   106004.0078125    69536.5234375 ]
 [  33989.9375       11847.11425781   12255.80859375]
 [  12526.203125     58372.3046875    37641.34765625]
 ..., 
 [  98823.2734375    95837.1796875    18758.7734375 ]
 [  27047.19140625   74242.859375     78166.703125  ]
 [  19353.97851562   82375.8515625     1147.07556152]]

Yes, they are the same shape.

My attempt,

import numpy as np

xs1,ys1,zs1 = zip(*MW_FirstsubPos1)
xs11,ys11,zs11 = zip(*MW_SecondsubPos1)

squared_dist1 = (xs11 - xs1)**2 + (ys11 - ys1)**2 + (zs11 - zs1)**2
dist1 = np.sqrt(squared_dist1)

print dist1

This returns:

TypeError: unsupported operand type(s) for -: 'tuple' and 'tuple'

I'm just wanting to return a 1-D array of the same shape.

* --------------------- Update --------------------- *

Using what Sнаđошƒаӽ said,

Distance1 = []
for Fir1, Sec1 in zip(MW_FirstsubVel1, MW_SecondsubPos1):
    dist1 = 0
    for i in range(3):
        dist1 += (Fir1[i]-Sec1[i])**2
    Distance1.append(dist1**0.5)

But when comparing the distance formula for one element in my original post such as,

squared_dist1 = (xs11[0] - xs1[0])**2 + (ys11[0] - ys1[0])**2 + (zs11[0] - zs1[0])**2
dist1 = np.sqrt(squared_dist1)

print dist1

returns 322.178309762

while

result = []
for a, b in zip(MW_FirstsubVel1, MW_SecondsubPos1):
    dist = 0
    for i in range(3):
        dist += (a[i]-b[i])**2
    result.append(dist**0.5)

print result[0]

returns 137163.203004

What's wrong here?

1
  • Hei, my solution gives that expected solution too. Take a look at my updated answer that shows the outputs. Commented Jul 20, 2016 at 4:33

3 Answers 3

2

Your solutions look good to me. A better idea is to use the linear algebra module in scipy package, as it scales with multiple dimensional data. Here are my codes.

import scipy.linalg as LA

dist1 = LA.norm(MW_FirstsubPos1 - MW_SecondsubPos1, axis=1)
Sign up to request clarification or add additional context in comments.

1 Comment

My solution does not work unfortunately. I've added more to the issue.
2

See if this works, assuming that aaa and bbb are normal python list of lists having the x, y and z coordinates (or that you can convert to such, using tolist or something like that perhaps). result will have the 1-D array you are looking for.

Edit: aaa and bbb are python lists of lists. Only code for printing the output have been added.

aaa = [[51618.7265625, 106197.7578125, 69647.6484375],
[33864.1953125, 11757.29882812, 11849.90332031],
[12750.09863281, 58954.91015625, 38067.0859375],
[99002.6640625, 96021.0546875, 18798.44726562],
[27180.83984375, 74350.421875, 78075.78125],
[19297.88476562, 82161.140625, 1204.53503418]]

bbb = [[51850.9140625, 106004.0078125, 69536.5234375],
[33989.9375, 11847.11425781, 12255.80859375],
[12526.203125, 58372.3046875, 37641.34765625],
[98823.2734375, 95837.1796875, 18758.7734375],
[27047.19140625, 74242.859375, 78166.703125],
[19353.97851562, 82375.8515625, 1147.07556152]]

result = []
for a, b in zip(aaa, bbb):
    dist = 0
    for i in range(3):
        dist += (a[i]-b[i])**2
    result.append(dist**0.5)

for elem in result:
    print(elem)

Output:

322.178309762234
434.32361222259755
755.5206249710258
259.9327309143388
194.16071591842936
229.23543894772612

2 Comments

I appreciate the response. Trying what you posted left me at a hurdle. I've posted it on the update potion.
@DarthLazar The way I understood the question is that the two arrays hold pairs of points that we need to find the distance between in the same indices. It seems now, after the update and my answer being unaccepted, that so is not the case. :-)
1

Here's a vectorized approach using np.einsum -

diffs = MW_FirstsubPos1 - MW_SecondsubPos1
dists = np.sqrt(np.einsum('ij,ij->i',diffs,diffs))

Sample run -

In [233]: MW_FirstsubPos1
Out[233]: 
array([[2, 0, 0],
       [8, 6, 1],
       [0, 2, 8],
       [7, 6, 3],
       [3, 1, 7]])

In [234]: MW_SecondsubPos1
Out[234]: 
array([[3, 4, 7],
       [0, 8, 4],
       [4, 7, 4],
       [2, 5, 6],
       [5, 0, 6]])

In [235]: diffs = MW_FirstsubPos1 - MW_SecondsubPos1

In [236]: np.sqrt(np.einsum('ij,ij->i',diffs,diffs))
Out[236]: array([ 8.1240384 ,  8.77496439,  7.54983444,  5.91607978,  2.44948974])

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.