我有一個Zc包含 n 個元素的一維向量,這些元素是二維數(shù)組。我想找到等于的每個二維數(shù)組的索引np.ones(Zc[i].shape)。a = np.zeros((5,5))b = np.ones((5,5))*4c = np.ones((5,5))d = np.ones((5,5))*2Zc = 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. 上面的代碼有效并返回正確的答案,但我想知道是否有一種矢量化的方式來實現(xiàn)相同的結果?
1 回答

白板的微信
TA貢獻1883條經(jīng)驗 獲得超3個贊
離開 hpaulj 的評論:
>>> allones = (Zc == np.array(np.ones(Zc[i].shape))).all(axis=(1,2))
>>> np.where(allones)[0][0]
2
添加回答
舉報
0/150
提交
取消