Is anyone familiar with a faster way to increment spans of indexes, as done by the following patch(ar, ranges)
? In other words, it counts overlapping ranges, somewhat like a histogram. Maybe there is a vectorized method that already does something similar?
import numpy as np
ar = np.zeros(10)
ranges = [(1, 2), (4, 7), (6, 8)]
add_this = 1
def patch(ar, ranges):
for start, end in ranges:
ar[start:end] += add_this
return ar
print np.arange(10)*1.
print patch(ar, ranges)
Output:
[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
[ 0. 1. 0. 0. 1. 1. 2. 1. 0. 0.]
My problem is similar to: - Pandas: Overlapping time count - How to identify the maximum number of overlapping date ranges?