我想要一个 boolean numpy 数组 fixidx
,它是比较 numpy 数组 a
、b
、c
和 d
。例如我有数组
a = np.array([1, 1])
b = np.array([1, 2])
c = np.array([1, 3])
d = np.array([1, 4])
因此数组 fixidx
具有值
fixidx = [1, 0]
我的方法是
fixidx = (a == b) & (b == c) & (c == d)
这在 Matlab 中有效,但事实证明 Python 只输出一个 ValueError。
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
any
或 all
不会成功,或者至少我无法弄清楚。
最佳答案
代码完美运行,没有错误。尝试将 boolean 输出转换为整数:
((a == b) & (b == c) & (c == d)).astype(int)
array([1, 0])
关于python - 比较两个以上的 numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52114666/