I need to bin some data, it takes too much time with my caveman coding, so I need some optimization. I have a time array and a magnitude array, I need to store multiple magnitude values of according to their time data. For example bin the magnitude values between 0-2 sec, then 2-4,4-6 and so on. My code is:
for i in range(time_values.shape[0]): ----> around 1000, so 1000+ elif statement
if (time[i] > 0 ) and ( 0.001 > time[i]):
bins['bin_'+str( 0 )].append(magnitude[i])
elif (time[i] > 0.001 ) and ( 0.002 > time[i]):
bins['bin_'+str( 1 )].append(magnitude[i])
elif (time[i] > 0.002 ) and ( 0.003 > time[i]):
bins['bin_'+str( 2 )].append(magnitude[i])
...
...
...
As you can see, I used a dictionary to bin the data, thinking it would be easier to create around 1000 empty bin array. But I'm starting to regret this since its taking very long. Is there a faster way of doing this job?