首页 > 解决方案 > 如何将 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

标签: pythonpython-3.xmatlab

解决方案


findPython中的 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 编写。


推荐阅读