MATLAB 中的 find(x==1) 究竟是什么 return 以及在 python 中与它等效的是什么?

What does find(x==1) in MATLAB exactly return and what is equivalent to it in python?

我正在尝试执行以下 MATLAB 代码在 python 中执行的操作,但我不太理解它的输出。这是 MATLAB 代码

A = zeros((48,60));

A(3,5) = 1;
A(3,6) = 1;
A(48,60) = 1;
A(47,60) = 1;
A(24,30) = 1;
A(38,45) = 1;

f = find(A == 1);

最后一行f returns [195;243;1416;2150;2879;2880]是一列数字。但我不明白这些数字是如何从 find() 获得的。似乎它没有将行号和列号相乘。为什么我得到这些数字?另外,我应该怎么做才能在 python 中得到同样的东西?

A= np.zeros(48, 60)
A[2, 4] = 1
A[2, 5] = 1
A[47, 59] = 1
A[46, 59] = 1
A[23, 29] = 1
A[37, 44] = 1

np.where(A ==1)

我试过 np.where(A== 1) 但它只有 returns 行和列的索引。

matlab中的find命令returns每个元素的线性索引。更多信息可以在这里找到:https://www.mathworks.com/help/matlab/ref/find.html#budquyz-1.

等效的 python 代码将是 np.argwhere,它也是 returns 索引:https://numpy.org/doc/stable/reference/generated/numpy.argwhere.html

要获得与 matlab 完全相同的行为,您必须将矩阵转换为向量:

A= np.zeros((48, 60))
A[2, 4] = 1
A[2, 5] = 1
A[47, 59] = 1
A[46, 59] = 1
A[23, 29] = 1
A[37, 44] = 1

np.argwhere(np.ravel(A)==1)