I am trying to replace part of a 2D numpy array named "S" as a function of i and j. Given S as:
>>> S
Out[1]:
array([[ 1., 0., 0.],
[ 0., 3., 0.],
[ 0., 0., 9.]]
for i= 0 and j= 1, I can access elements row i and j and column i and j using the following syntax:
>>> S[:, [i, j]][[i, j], :]
Out[2]:
array([[ 1., 0.],
[ 0., 3.]])
Now when I try to replace the same elements of array S with another array of same dimensions (tmp_arr) python does not give an error but it also does not do anything meaning that the elements of S remain unchanged and no error message is displayed.
>>> tmp_arr
Out[3]:
array([[ 555., 0.],
[ 0., 555.]])
>>> S[:, [i, j]][[i, j], :] = tmp_arr
and what I get is the same matrix:
>>> S
Out[4]:
array([[ 1., 0., 0.],
[ 0., 3., 0.],
[ 0., 0., 9.]])
Obviously the following would work but I am looking for an elegant solution:
S[i, i] = tmp_arr[0, 0]
S[i, j] = tmp_arr[0, 1]
S[j, i] = tmp_arr[1, 0]
S[j, j] = tmp_arr[1, 1]
I appreciate your comments and experiences.