Home > other >  Calculations with 3 differents list
Calculations with 3 differents list

Time:06-05

I am with a problem with calculations with 3 differents list.

I have this lists:

center_x= [1.340, 8.54065065, 3.718, 5.83]
center_y= [1.672, 1.67, 7.47, 4.71]
center_z= [-0.451, -1.389, 7.523, 8.15]

Each list has a spatial coordinate, that is, center_x[1], center_y[1], center_z[1], form a coordinate of type (x,y,z).

I need to calculate the distances between these coordinates, like this pairs 1-2, 2-3, 3-4 . I tried like this:

for i in range(len(centro_x)):
    print( 'Dists :',(( ((center_x[i 1]-center_x[i]**2)  center_y[i 1]-center_y[i]**2)  (center_z[i 1]-center_z[i]**2)) **(1/2)) )

But only be able to calculate for the first pair, 1-2.

And than obtain this error:

dist 3.0513998021801334
dist (6.261343210163308e-16 10.225549463768164j)
dist (6.351600500021062e-16 10.372950804171936j)
Traceback (most recent call last):
File "/home/josepinto/Desktop/quadrangular/auxiliar.py", line 81, in <module>
print(  'dist',(( ((centro_x[i 1]-centro_x[i]**2)  centro_y[i 1]-centro_y[i]**2)  
(centro_z[i 1]-centro_z[i]**2)) **(1/2)) )
IndexError: list index out of range

Someone can help me? Thanks

CodePudding user response:

The parenthesis are placed wrong and the range of your for loop is slightly mistaken to compute the distances between coordinates:

centro_x= [1.340, 8.54065065, 3.718, 5.83]
centro_y= [1.672, 1.67, 7.47, 4.71]
centro_z= [-0.451, -1.389, 7.523, 8.15]

for i in range(len(centro_x)-1):
    print( 'Dists :',((centro_x[i 1]-centro_x[i])**2   (centro_y[i 1]-centro_y[i])**2  (centro_z[i 1]-centro_z[i])**2) ** (1/2)  )

Since you are using i 1 in your print statement the error is thrown when it gets to the last element in centro_x, as i 1 from there does not refer to a valid position in the array; hence the error

Eventually, you could use the numpy.linalg.norm to compute the distance without a for loop

centro_x= [1.340, 8.54065065, 3.718, 5.83]
centro_y= [1.672, 1.67, 7.47, 4.71]
centro_z= [-0.451, -1.389, 7.523, 8.15]
import numpy as np
centro= np.array([[1.340, 8.54065065, 3.718, 5.83],[1.672, 1.67, 7.47, 4.71],[-0.451, -1.389, 7.523, 8.15]])
dist = np.linalg.norm(np.diff(centro),axis=0)
print(dist)

CodePudding user response:

You get this error because of the 1 in the index try:

for i in range(1,len(center_x)):
    print( 'Dists :',(( ((center_x[i-1]-center_x[i]**2)  center_y[i-1]-center_y[i]**2)  (center_z[i-1]-center_z[i]**2)) **(1/2)) )

my code:

center_x= [1.340, 8.54065065, 3.718, 5.83]
center_y= [1.672, 1.67, 7.47, 4.71]
center_z= [-0.451, -1.389, 7.523, 8.15]
for i in range(1,len(center_x)):
    print( 'Dists :',(( ((center_x[i-1]-center_x[i]**2)  center_y[i-1]-center_y[i]**2)  (center_z[i-1]-center_z[i]**2)) **(1/2)) )

CodePudding user response:

You're out of range IndexError: list index out of range since you start with first index [0] and reach the 5th index [4] which is not in the list. So you have to change this line

for i in range(len(center_x)):

to

for i in range(len(center_x) - 1):

another hint: there's a parenthesis problem placed here in the formula you wrote, which need to be written correctly to avoid miscalculations.

  • Related