python - 如何将 MATLAB 的命令 find 转换为 Python?
问题描述
我在 Matlab 的一个程序中进行了此迭代,并希望将其转换为 Python,但我的问题在于“n”和“direction”的参数。
for i=1:size(labels)
idx_V=[idx_V;find(y(idxUnls(trial,:))==labels(i),l/length(labels),'first')]
end
解决方案
find
Python中的 MATLAB 函数没有一对一的交换。从此处的另一个答案中汲取灵感,我将提出以下解决方案:
% MATLAB
inds = find(myarray == condition, n, 'first')
# Python
import numpy as np
inds = [ind for (ind, val) in np.ndenumerate(myarray == condition) if val]
inds = inds[0:n]
我敢肯定find
,与ndenumerate
. Python 表达式也可以构造为生成器。
如果您想要类似的实现,则必须自己用 Python 编写。
推荐阅读
- javascript - 如何在 antd select 上添加分页?因为从接口获取数据是巨大的。所以我想实现分页
- python - Python 3.6 tkinter 说 GROOVE 没有定义
- google-cloud-platform - SyntaxError:位置参数跟随关键字参数 | 用于语音到文本的 Python 客户端
- angular - PrimeNg 柱过滤
- microsoft-teams - 在管理中心启用应用程序不可用。如何更改设置?
- delphi - 更改 Delphi 10 自动生成的文件(identcache、dof、cfg...)
- c# - EPPlus - InsertRow:行不能小于 1。参数名称:值
- kubernetes - kubectl get nodes 命令出错
- android - Firebase 性能监控显示 0 个会话
- azure-service-fabric - 将 dll 复制到 azure VM 规模集以供其他服务在运行时使用