如何在numpy中对张量的矩阵执行按位运算

时间:2019-04-30 20:55:53

标签: python numpy matrix bitwise-operators logical-operators

我有以下numpy张量:

M = np.zeros((a,b,c), dtype=bool)

我希望对尺寸为a的所有b,c矩阵进行按位运算 给出尺寸为b,c的最终矩阵。我不知道如何实现 有效率的。像

np.apply_along_axis(func1d=np.bitwise_and, axis=0, arr=M) 但我收到错误消息:ValueError: invalid number of arguments 我还不清楚为什么。

更新:这可行,但是还有一种(时间)更有效的方法吗?

v = np.ones((b,c),dtype=bool)
for i in range(0, a):
  v = v & M[i]

1 个答案:

答案 0 :(得分:1)

您可以为此使用all

>> M = np.zeros((8,9,10), dtype=np.bool)
>> M.all(0).shape
(9, 10)