Given a numpy sized array nand an integer m, I want to generate all consecutive subsequences of the length of the marray, preferably as a two-dimensional array.
Example:
>>> subsequences(arange(10), 4)
array([[0, 1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6, 7],
[2, 3, 4, 5, 6, 7, 8],
[3, 4, 5, 6, 7, 8, 9]])
the best way i can do this is
def subsequences(arr, m):
n = arr.size
indices = cumsum(vstack((arange(n - m + 1), ones((m-1, n - m + 1), int))), 0)
return arr[indices]
Is there a better, preferably built-in function that I miss?