我希望寻求更好的方法对矩阵行中所有元素进行按位与运算。
我有一个数组:
import numpy as np
A = np.array([[1,1,1,4], #shape is (3, 5) - one sample
[4,8,8,16],
[4,4,4,4]],
dtype=np.uint16)
B = np.array([[[1,1,1,4], #shape is (2, 3, 5) - two samples
[4,8,8,16],
[4,4,4,4]],
[[1,1,1,4],
[4,8,8,16],
[4,4,4,4]]]
dtype=np.uint16)
例子及期望输出:
resultA = np.bitwise_and(A, axis=through_rows) # doesn't work
# expected output should be a bitwise and over elements in rows resultA:
array([[0],
[0],
[4]])
resultB = np.bitwise_and(B, axis=through_rows) # doesn't work
# expected output should be a bitwise and over elements in rows
# for every sample resultB:
array([[[0],
[0],
[4]],
[[0],
[0],
[4]]])
但是我的输出结果是:
resultA = np.bitwise_and(A, axis=through_rows) # doesn't work
File "<ipython-input-266-4186ceafed83>", line 13
dtype=np.uint16)
^
SyntaxError: invalid syntax
因为 numpy.bitwise_and(x1, x2[, out]) 需要两个数组作为输入,那么我该如何得到我期望的输出呢?
axis=through_rows
?你期望的输出结果是什么? - juanpa.arrivillaga