如何检查矩阵是否包含零列? [英] How to check that a matrix contains a zero column?
本文介绍了如何检查矩阵是否包含零列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个大矩阵,我想检查一下其中是否有一个全零的列.如何在numpy中做到这一点?
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?
推荐答案
这是一种方法:
In [19]: a
Out[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])
这篇关于如何检查矩阵是否包含零列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文