python - 检查 numpy.nonzero 的空结果的优雅方法

标签 python matlab numpy code-translation

使用 numpy 函数 numpy.nonzero,是否有一种优雅的方法来检查作为输出的元组是否为空数组?

在 MATLAB 中,这很容易

answer = find( matrix_a < matrix_b );
isempty(answer)

最佳答案

这样做的 numpythonic 方法是直接在 ndarray 对象上使用 any/all 方法。

在您的示例中,您的代码会询问:没有索引 matrix_a 小于 matrix_b 吗?

not (matrix_a < matrix_b).any()

等价地,matrix_a是否所有元素都大于matrix_b中对应的元素?

(matrix_a >= matrix_b).all()

关于python - 检查 numpy.nonzero 的空结果的优雅方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30042919/

相关文章:

image - 将角度定量数据转换为定性图像

c++ - 如何通过Matlab Mex编译工具使用C++编译器

Python-从二进制数据读取二维数组

python - numpy 数组整数索引多于一维

python - pysvn.ClientError : WC locked with sqlite attempt to write a readonly database during checkout to a same directory in the second time

python - 如何在 psycopg2 中返回列表而不是元组

python - 使用 Xarray 迭代创建 DataArray 的最直接/紧凑的方法

python - 在 Pandas DataFrame 中使用 set_index

matlab - 创建水平拉伸(stretch)的上三角矩阵

python - 在 python 中从矩阵创建网格