DarthLazar - 4 months ago 16

Python Question

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.

Answer

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.

```
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)
```