Skip to content Skip to sidebar Skip to footer

How To Check That A Matrix Contains A Zero Column?

I have a large matrix, I'd like to check that it has a column of all zeros somewhere in it. How to do that in numpy?

Solution 1:

Here's one way:

In[19]: aOut[19]: 
array([[9, 4, 0, 0, 7, 2, 0, 4, 0, 1, 2],
       [0, 2, 0, 0, 0, 7, 6, 0, 6, 2, 0],
       [6, 8, 0, 4, 0, 6, 2, 0, 8, 0, 3],
       [5, 4, 0, 0, 0, 0, 0, 0, 0, 3, 8]])

In[20]: (~a.any(axis=0)).any()
Out[20]: True

If you later decide that you need the column index:

In [26]: numpy.where(~a.any(axis=0))[0]
Out[26]: array([2])

Solution 2:

Create an equals 0 mask (mat == 0), and run all on it along an axis.

(mat == 0).all(axis=0).any()

Post a Comment for "How To Check That A Matrix Contains A Zero Column?"