Fast rolling-sum for list of data vectors (2d matrix)
I am looking for a fast way to compute a rolling-sum, possibly using Numpy. Here is my first approach: def func1(M, w): Rtn = np.zeros((M.shape[0], M.shape[1]-w+1)) for i in range(M.shape[1]-w+1): Rtn[:,i] = np.sum(M[:, i:w+i], axis=1) return Rtn M = np.array([[0., 0., 0., 0., 0., 1., 1., 0., 1., 1., 1., 0., 0.], [0., 0., 1., 0., 1., 0., 0., 0., 0., 0., 0., 1., 1.], [1., 1., 0., 1., 0., 0., 0., 1., 0., 0., 0., 0., 0.]]) window_size = 4 print func1(M, window_size) [[ 0. 0. 1. 2. 2. 3. 3. 3. 3. 2.] [ 1. 2. 2. 1. 1. 0. 0. 0. 1. 2.] [ 3. 2. 1. 1. 1. 1. 1. 1. 0. 0.]] I wanted to prevent the window