I have 0
s and 1
s store in a 3-dimensional numpy array:
g = np.array([[[0, 1], [0, 1], [1, 0]], [[0, 0], [1, 0], [1, 1]]])
# array([
# [[0, 1], [0, 1], [1, 0]],
# [[0, 0], [1, 0], [1, 1]]])
and I'd like to replace these values by those in another array using a row-wise replacement strategy. For example, replacing the vales of g
by x
:
x = np.array([[2, 3], [4, 5]])
array([[2, 3],
[4, 5]])
to obtain:
array([
[[2, 3], [2, 3], [3, 2]],
[[4, 4], [5, 4], [5, 5]]])
The idea here would be to have the first row of g
replaced by the first elements of x
(0
becomes 2
and 1
becomes 3
) and the same for the other row (the first dimension - number of "rows" - will always be the same for g
and x
)
I can't seem to be able to use np.where
because there's a ValueError: operands could not be broadcast together with shapes (2,3,2) (2,2) (2,2)
.