I have two arrays P and T. P[i] is a number, whose time stamp is T[i]; There might be duplicated time stamps.
I want to produce another two arrays Q and U, where Q[i] has time stamp U[i], and Q[i] is the sum of all elements in P that have time stamp U[i];
For example, for
P = [1, 2, 3, 4, 5]
T = [0, 0, 1, 1, 1]
I will produce
Q = [3, 12]
U = [0, 1];
Is there a fast way of doing this in numpy, that hopefully vectorizes it?
Using numpy 1.4 or better:
Please note that this is not the fastest solution. I tested the speed this way:
For small arrays, wim’s solution is faster (N=1):
For large arrays, joris’s solution is faster (N=1000):
I doubt it matters in this case, but benchmarks can change depending on version of numpy, python, OS, or hardware. It would not hurt to repeat these benchmarks on your machine.