I am trying to split a 2D numpy array that is not square into nonoverlapping chunks of smaller 2D numpy arrays. Example - split a 3x4 array into chunks of 2x2:
The array to be split:
[[ 34 15 16 17]
[ 78 98 99 100]
[ 23 78 79 80]]
This should output:
[[34 15]
[78 98]]
[[ 16 17]
[ 99 100]]
So [23 78 79 80] are dropped because they do not match the 2x2 requirement.
My current code is this:
new_array = np.array([[34,15,16,17], [78,98,99,100], [23,78,79,80]])
window = 2
for x in range(0, new_array.shape[0], window):
for y in range(0, new_array.shape[1], window):
patch_im1 = new_array[x:x+window,y:y+window]
This outputs:
[[34 15]
[78 98]]
[[ 16 17]
[ 99 100]]
[[23 78]]
[[79 80]]
Ideally, I would like to have the chunks stored in a list.