Suppose I have an array [1,2,3,4,5,6,7,8]
, and the array is composed of two samples [1,2,3,4]
, and [5,6,7,8]
. For each sample, I want to do a slicing window with window size n
. And if there are not enough elements, pad the result with the last elements. Each row in the return value should be the sliced window starting from the element in that row.
For example:
if n=3
, then the result should be:
[[1,2,3],
[2,3,4],
[3,4,4],
[4,4,4],
[5,6,7],
[6,7,8],
[7,8,8],
[8,8,8]]
How can I achieve this with efficient slicing instead of a for loop? Thanks.