Say I have the following 3D array:
L=np.arange(18).reshape((2,3,3))
L[:,:,1] = 0; L[:,[0,1],:] = 0
In []: L
Out[]:
array([[[ 0, 0, 0],
[ 0, 0, 0],
[ 6, 0, 8]],
[[ 0, 0, 0],
[ 0, 0, 0],
[15, 0, 17]]])
where zero columns in L[0,:]
are always matched by corresponding zero columns in L[1,:]
.
I want to now remove the middle columns where the sum along the axis equals 0 (ignoring rows of zero. My current clumsy approach is
l=np.nonzero(L.sum(axis=1))[1]
In []: L[:,:,l[:len(l)/2]]
Out[]:
array([[[ 0, 0],
[ 0, 0],
[ 6, 8]],
[[ 0, 0],
[ 0, 0],
[15, 17]]])
What is a less roundabout way of doing this?