I wanna index a 2d ndarray and fetch a subset of this ndarray. Then I assign this subset ndarray with another ndarry which has the same shape with this subset ndarray. But the original ndarray is not changed.
Here is an example.mat
is not changed after the assignment operation.
What I want is assign a subset of ndarray
with another ndarray.
import numpy as np
ma = np.array([1,2,3,4,5] * 5).reshape(5,5)
mask = np.array([False, True, False, True, True])
sub = np.array([[100,101, 102],
[103, 104, 105],
[106,107,108]])
ma[mask][:,mask] = sub
print(ma)
what I expected is:
array([[1, 1, 3, 1, 5],
[1, 100, 1, 101, 102],
[1, 1, 3, 1, 5],
[1, 103, 3, 104, 105],
[1, 106, 3, 107, 108]])
But mat is not changed:
array([[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5]])