Numpy where 沿轴 0 的条件语句

Numpy where conditional statement along axis 0

我有一个一维向量 Zc,其中包含 n 个二维数组元素。我想找到等于 np.ones(Zc[i].shape) 的每个二维数组的索引。

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 

其中 returns 2。上面的代码有效 returns 正确答案,但我想知道是否有矢量化方法可以达到相同的结果?

离开 hpaulj 的评论:

>>> allones = (Zc == np.array(np.ones(Zc[i].shape))).all(axis=(1,2))
>>> np.where(allones)[0][0]
2