This question is an extension to my original post earlier
Numpy - How to filter and shorten a 3d array?
Suppose I have the following 6 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]],
[[-2, -2, -2],
[-2, -1, -2],
[74, 78, -2]]
[[-2, -2, -2],
[-2, -2, -2],
[-2, -2, -2]]])
Now I would like to filter such array by the following standards:
Treat each 3 by 3 array as a block, if all elements in this block = -2, we take this block as 'empty', we should only cut the block which are consecutively 'empty' from the start and from the end until there occurs a non-'empty' block, so the target array would look like this(3 by 3 by 3):
array([[[-2, -2, 71],
[-1, -1, -1],
[71, -1, 52]],
[[-2, -2, -2],
[-2, -2, -2],
[-2, -2, -2]],
[[-2, -2, -2],
[-2, -1, -2],
[74, 78, -2]]])
You can reproduce the array by
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],
[-2,-2,83,-2,-76,-2,-87,-2,-2],
[-2,-2,-2,-2,-2,-2,-2,-2,-2]])
newarr = array.reshape(6,3,3)