首页 > 解决方案 > python numpy中的sum(条件)等价物

问题描述

我正在尝试将一段 matlab 代码转换为 python。

a=[1 2 3;4 5 6]
b= sum(a<5)
//output :
ans :
2 1 1

实际上返回具有条件的每一列中的元素数。numpy(python)中是否有任何等效函数可以做到这一点?

标签: pythonmatlabnumpy

解决方案


一样的。

a=np.array([[1, 2, 3],[4, 5, 6]])
b=np.sum(a<5,axis=0) # the only difference is that you need to explicitly set the dimension

推荐阅读