python - python numpy中的sum(条件)等价物
问题描述
我正在尝试将一段 matlab 代码转换为 python。
a=[1 2 3;4 5 6]
b= sum(a<5)
//output :
ans :
2 1 1
实际上返回具有条件的每一列中的元素数。numpy(python)中是否有任何等效函数可以做到这一点?
解决方案
一样的。
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
推荐阅读
- delegates - 将值从一个 subviecontroller 快速传递到情节提要中的 mainviewcontroller
- wordpress - 未登录时首页白屏
- java - Springboots 允许保存重复的文档
- reactjs - 从 dispatch 中确定返回值的类型
- android - CompositeDisposable.clear() 不会停止 Observable 的执行
- microsoft-graph-api - Microsoft Graph API 在 calendarView 上使用 nextLink 导致 ErrorAccessDenied
- r - 如何从 R 中未定义和未知的点空间序列制作 SpatialLines 对象?
- wpf - Showing loading animation while dgv loads
- r - 如何在 R 中创建 R 降价文件列表?
- javascript - Firebase Auth:无法删除从 currentUser.delete() 返回的 Promise 中的 Firestore 文档