I am looking for the numpythonic way to accomplish the following:
A = np.arange(1000)
x = np.array([0, 10, 20, 30], dtype=int)
dx = np.array([3, 4, 5, 6], dtype=int)
for x_, dx_ in zip(x, dx):
print(A[x_:x_+dx_])
Looking at similar answers and the as_strided documentation it doesn't seem like I can provide varying stride/window lengths. Is there a pure numpy way to accomplish this?
As @NickOdell pointed out, Numpy does not support jagged arrays. However if you can afford to waste some memory, there are workarounds. For example you could use
np.nanas a fill value:Which prints:
Whether this is a valid workaround depends on your context.
Otherwise you might want to take a look at https://awkward-array.org, which supports jagged arrays.
I hope this helps!