Suppose I have the following 4 by 3 by 3 array,
array([[[-2, -2, -2],
[-2, -2, -2],
[-2, -2, -2]],
[[-2, -2, -2],
[-2, -2, -2],
[-2, -2, -2]],
[[-2, -2, 71],
[-1, -1, -1],
[71, -1, 52]],
[[-2, -2, -2],
[-2, -2, -2],
[-2, -2, -2]]])
I would like to filter such array by the following standard:
Treat each 3 by 3 array as a block. If all elements in this block are equal to -2, we should cut the whole block, so the target array would look like this (1 by 3 by 3):
array([[[-2, -2, 71],
[-1, -1, -1],
[71, -1, 52]]])
I could only come up with a brute force solution with an explicit if condition and a for loop, but it does not work. Can anyone share a better method?
You can recreate the original array by the following commands
array = np.array([[-2,-2,-2,-2,-2,-2,-2,-2,-2],
[-2,-2,-2,-2,-2,-2,-2,-2,-2],
[-2,-2,71,-1,-1,-1,71,-1,52],
[-2,-2,-2,-2,-2,-2,-2,-2,-2]])
newarr = array.reshape(4,3,3)