在成为MATLAB用户多年之后,我现在正在迁移至python。
我尝试找到一种简洁的方式来简单地在python中重写以下MATLAB代码:
s = sum(Mtx);
newMtx = Mtx(:, s>0);
其中Mtx是2D稀疏矩阵
我的python解决方案是:
s = Mtx.sum(0)
newMtx = Mtx[:, np.where((s>0).flat)[0]] # taking the columns with nonzero indices
其中Mtx是2D CSC稀疏矩阵
python代码不像在matlab中那样可读/优雅。任何想法如何更优雅地编写它?
谢谢!
由于rayryeng的领导,找到了一个简明的答案:
s = Mtx.sum(0)
newMtx = Mtx[:,(s.A1 > 0)]
另一种选择是:
s = Mtx.sum(0)
newMtx = Mtx[:,(s.A > 0)[0]]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句