Suppose I have the following numpy array:
>>> a = np.arange(0,21,1)
>>> a
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])
Now suppose that I want to pick a window of length N
, where 2 < N <= a.shape[0]
, such that the window is "centered" around one of the elements of the array a
. For example, if I want to center a window of length N = 5
around the element 10
in array a
, then this window would be:
>>> idx = 10 # index of the array element 10
>>> N = 5 # window size
>>> a[idx - N//2:idx + N//2 + 1]
array([ 8, 9, 10, 11, 12])
This method generalizes well for windows that are not near the edges of the array, but I can't make it work otherwise. For example, if I want to extract a window of length N = 7
around the element 2
in a
, then what I get is:
>>> idx = 2
>>> N = 7
>>> a[idx - N//2:idx + N//2 + 1]
array([], dtype=int32)
However what I want is:
>>> a[0:7]
array([0, 1, 2, 3, 4, 5, 6])
How can I generalize this method for windows near the edges of a
?