我有一个 1D 向量 Zc
,其中包含 n 个二维数组元素。我想找到每个等于 np.ones(Zc[i].shape) 的 2D 数组的索引。
a = np.zeros((5,5))
b = np.ones((5,5))*4
c = np.ones((5,5))
d = np.ones((5,5))*2
Zc = np.stack((a,b,c,d))
for i in range(len(Zc)):
a = np.ones(Zc[i].shape)
b = Zc[i]
if np.array_equal(a,b):
print(i)
else:
pass
返回2
。上面的代码有效并返回正确的答案,但我想知道是否有矢量化的方法来实现相同的结果?
最佳答案
脱离 hpaulj 的评论:
>>> allones = (Zc == np.array(np.ones(Zc[i].shape))).all(axis=(1,2))
>>> np.where(allones)[0][0]
2
关于python - Numpy where 条件语句沿轴 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52302458/