Skip to content Skip to sidebar Skip to footer

Moving Window Sum On A Boollean Array, With Steps.

I'm struggling with creating a moving window sum function that calculates the number of True values in a given numpy Boolean array my_array, with a window size of n and in jumping

Solution 1:

Since you tagged numpy:

my_array = [True, True, False, False, True, False, True]
n = 2s = 2result = np.convolve(my_array, np.ones(n, ), mode='valid')[::s]

Solution 2:

Straight forward. Following code should do.

deffun(arr, n, s):
    res = []

    for i inrange(0, len(arr), s):
        res.append(sum(arr[i:i+n]))

    return res

my_array = [True, True, False, False, True, False, True]
print(fun(my_array, 2, 2))

Post a Comment for "Moving Window Sum On A Boollean Array, With Steps."