Removing nested for loop to find coincidence values

2024/10/14 3:21:09

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 time consuming. I was told that a better way might be to sort the two arrays based on the data, but this requires me to combine several 1D arrays and one multi-D array, sort based on one column, then separate them again. Is there a more efficient way of doing this? Here is a sample of my code:

x1 = []
x2 = []
velocity = []
plane1Times = np.array([[2293902],[2848853],[482957]])
plane2Times = np.array([[7416504],[2613113],[2326542]])
plane1Local = np.array([[0,0,0],[0,u,0],[0,2*u,0],[u,0,0],[u,u,0],[u,2*u,0],[2*u,0,0],[2*u,u,0],[2*u,2*u,0],[3*u,0,0],[3*u,u,0],[3*u,2*u,0]],dtype='float')
plane2Local = np.array([[0,0,D],[0,u,D],[0,2*u,D],[u,0,D],[u,u,D],[u,2*u,D],[2*u,0,D],[2*u,u,D],[2*u,2*u,D],[3*u,0,D],[3*u,u,D],[3*u,2*u,D]],dtype='float')
for i in range(0,len(plane1Times)):tic = time.time()for n in range(0,len(plane2Times)):if plane2Times[n] - plane1Times[i] <= 10000 and plane2Times[n] - plane1Times[i] > 0:x1 = plane1Local[plane1Dets[i]]x2 = plane2Local[plane2DetScale[n]]distance = np.sqrt((x2[0]-x1[0])**2 + (x2[1]-x1[1])**2 + (x2[2])**2)timeSeparation = (plane2Times[n]-plane1Times[i])*timeScalevelocity += distance/timeSeparationbreak

To give you an example of the time it is currently taking, each array of times is 10**6 values long so 100 loops in i takes about 60 seconds. Can someone please help me?

Answer

I cant really test because the code you provided isn't complete, but this is a possible solution

for index,value in enumerate(plane1Times):vec = plane2Times - valuerow,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]) * timeScalevelocity += 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.

https://en.xdnf.cn/q/118004.html

Related Q&A

Combine two pandas DataFrame into one new

I have two Pandas DataFrames whose data from different sources, but both DataFrames have the same column names. When combined only one column will keep the name.Like this:speed_df = pd.DataFrame.from_d…

Reasons of slowness in numpy.dot() function and how to mitigate them if custom classes are used?

I am profiling a numpy dot product call. numpy.dot(pseudo,pseudo)pseudo is a numpy array of custom objects. Defined as:pseudo = numpy.array([[PseudoBinary(1), PseudoBinary(0), PseudoBinary(1)],[PseudoB…

How to open cmd and run ipconfig in python

I would like to write a script that do something like that: open the cmd and run the commend "ipconfig" and than copy my ip and paste it to a text file. I wrote the beginning of the script …

Using OAuth to authenticate Office 365/Graph users with Django

We are creating an application for use in our organization, but we only want people in our organization to be able to use the app. We had the idea of using Microsofts OAuth endpoint in order to authent…

Python flatten array inside numpy array

I have a pretty stupid question, but for some reason, I just cant figure out what to do. I have a multi-dimensional numpy array, that should have the following shape:(345138, 30, 300)However, it actual…

Peewee and Flask : Database object has no attribute commit_select

Im trying to use Peewee with Flask, but I dont understand why my database connection does not work.config.pyclass Configuration(object): DATABASE = {name: test,engine: peewee.MySQLDatabase,user: root,p…

for loop to create a matrix in python

I am trying to study the probability of having a zero value in my data and I have developed a code that outputs the value of a column of data when the other is zero which is what I need. But having to …

How to convert List of JSON frames to JSON frame

I want to convert List of JSON object ot Single JSON frameHere is my codefor i in user1:name=i.namepassword=i.passwordid1=i.iduser = { "name" : name,"password" : password,"id&q…

Python 2.7 The packaging package is required; normally this is bundled with this package

I expect this has to do with the cryptography module, but Im not sure.Traceback (most recent call last):File "<string>", line 11, in <module>File "c:\python27\lib\site-packag…

Couple the data in all possible combinations

I have data in column in two columns like thisId Value 1 a 2 f 1 c 1 h 2 aand Id like couple the data of the Value column in all possible combinations based on the same Id such as(a,c) (a,h)…