删除2D矩阵中的全零行 [英] Remove all-zero rows in a 2D matrix

查看:69
本文介绍了删除2D矩阵中的全零行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个有效的和/或内置的函数来删除2d数组的全零行?我正在查看numpy文档,但尚未找到.

Is there an efficient and/or built in function to remove the all-zero rows of a 2d array? I am looking at numpy documentation but I have not found it.

推荐答案

布尔索引将做到这一点:

Boolean indexing will do it:

In [2]:

a
Out[2]:
array([[4, 1, 1, 2, 0, 4],
       [3, 4, 3, 1, 4, 4],
       [1, 4, 3, 1, 0, 0],
       [0, 4, 4, 0, 4, 3],
       [0, 0, 0, 0, 0, 0]])
In [3]:

a[~(a==0).all(1)]
Out[3]:
array([[4, 1, 1, 2, 0, 4],
       [3, 4, 3, 1, 4, 4],
       [1, 4, 3, 1, 0, 0],
       [0, 4, 4, 0, 4, 3]])

这篇关于删除2D矩阵中的全零行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆