Removing Nested For Loop To Find Coincidence Values
I am currently using a nested for loop to iterate through to arrays to find values that match a certain criterion. The problem is that this method is incredibly inefficient and tim
Solution 1:
I cant really test because the code you provided isn't complete, but this is a possible solution
forindex,value in enumerate(plane1Times):
vec = plane2Times - value
row,col = np.where((vec<=10000)&(vec>0))
if len(row) > 0:
x1 = plane1Local[plane1Dets[index]]
x2 = plane2Local[plane2DetScale[row[0]]]
distance = np.sqrt((x2[0] - x1[0]) ** 2 + (x2[1] - x1[1]) ** 2 + (x2[2]) ** 2)
timeSeparation = (plane2Times[row[0]] - plane1Times[index]) * timeScale
velocity += distance / timeSeparation
Eliminate the second loop, and just do the subtraction all at once. Then search the new array, where it meats your criteria. Since it seems that you want the first value, just take the first index like row[0]
to get the index of the value check. Removing the second for loop should drop the time considerably.
Post a Comment for "Removing Nested For Loop To Find Coincidence Values"