Say I have a square matrix as input :
array([[0, 1, 1, 0], [1, 1, 1, 1], [1, 1, 1, 1], [0, 1, 1, 0]])
I want to count nonzero in the array after deleting rows 2 and 3 and columns 2 and 3. Subsequently, I want to do the same for rows 3 and 4 and columns 3 and 4. Therefore, the output should be:
0
Here is a naive solution using np.delete :
import numpy as np a = np.array([[0,1,1,0],[1,1,1,1],[1,1,1,1],[0,1,1,0]]) np.count_nonzero(np.delete(np.delete(a, (1,2), axis=0), (1,2), axis=1)) np.count_nonzero(np.delete(np.delete(a, (2,3), axis=0), (2,3), axis=1))
But np.delete returns a new array. Is there a faster method that involves deleting rows and columns at the same time? Is it possible to mask? the documentation on np.delete reads:
It is often recommended to use a boolean mask.
How can I do it? Thanks.