0

I have a bool 2D-array A with the numbers of True being the dimension of bool 2D-array 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]]

I want to "overlay" B on the "True"-Array of A, so that I'd get

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

My ultimate goal is to manipulate an array

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

with something like

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

to get

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

Thanks in advance.

2 Answers 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
Sign up to request clarification or add additional context in comments.

Comments

0

The solution I came up with was (works only if B is quadratic):

arr[A] = (arr[A].reshape(len(B), len(B)) + 2 * B).ravel()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.