I have a 2D numpy array. I would like to modify it by creating an array of its diagonal elements and modifying the diagonal array so that those changes reflect on the original 2D array.
I have tried with:
>>> a = np.ones(shape=(3,3))
>>> d1 = a[np.diag_indices_from(a)]
>>> d1
array([1., 1., 1.])
>>> d1[0] = 2
>>> d1
array([2., 1., 1.])
>>>a
array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
It can be seen that the changes do not affect the original array.
Is there any way to create a diagonal array that will also affect the original 2D array?
EDIT: I get the effect I'm looking for when I work with rows or columns:
>>> row0 = a[0]
>>> row0[0] = 0
>>> a
array([[0., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
>>> column0=a[:,0]
>>> column0[2]=3
>>> a
array([[0., 1., 1.],
[1., 1., 1.],
[3., 1., 1.]])