Here are two arrays of equal length, one of which contains data, one of which contains results, but is initially set to zero, for example:
a = numpy.array([1, 0, 0, 1, 0, 1, 0, 0, 1, 1])
b = numpy.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
I would like to calculate the sum of all possible subsets of three adjacent elements in a. If the sum is 0 or 1, the three corresponding elements in b remain unchanged; only if the sum exceeds 1, then the three corresponding elements from b are set to 1, so that after calculating b becomes
array([0, 0, 0, 1, 1, 1, 0, 1, 1, 1])
A simple loop will do this:
for x in range(len(a)-2):
if a[x:x+3].sum() > 1:
b[x:x+3] = 1
After that, b has the desired shape.
I have to do this for a lot of data, so speed is a problem. Is there a faster way in NumPy to perform the operation above?
(I understand that this looks like a convolution, but not quite the same).