0

我有一个 bool 2D 数组 A,其中 True 的数字是 bool 2D 数组 B 的维数。

A = np.array([[False, True, True, False, True],[False, False, False, False, False],[False, True, True, False, True]])
B = np.array([[True, False, True],[True, True, True]])

A =[[False, True,  True,  False, True],
    [False, False, False, False, False],
    [False, True,  True,  False, True]]
B =[[True, False, True],
    [True, False, True]]

我想在 A 的“真”阵列上“覆盖”B,这样我就可以得到

C =  
[[False, **True**,  **False**,  False, **True**],  
[False, False, False, False, False],  
[False, **True**,  **False**,  False, **True**]]  

我的最终目标是操作数组

arr = [[1, 2 , 3 , 4, 5 ], [6,7,8,9,10], [11, 12 , 13 , 14, 15 ]]

有类似的东西

arr[A] = arr[A] + B*2

要得到

arr = [[1, 4 , 3 , 4, 7 ], [6,7,8,9,10], [11, 14 , 13 , 14, 17 ]]

提前致谢。

4

2 回答 2

1
# get the indexes that are True
Xi = np.nonzero(A)

# convert to an array of 1D
B1 = np.ndarray.flatten(B)

# use Xi for dynamic indexing
A[Xi]=B1
于 2020-02-23T12:28:49.127 回答
0

我想出的解决方案是(仅当 B 是二次方时才有效):

arr[A] = (arr[A].reshape(len(B), len(B)) + 2 * B).ravel()
于 2020-02-23T12:45:15.037 回答