I would like to set the diagonal of some numpy
matrix to an arbitrary 1D array.
For example, if:
mat = np.array([[1,2],[3,4]])
diag = np.array([5,6])
then:
>>> set_diagonal(mat, diag)
>>> mat
... array([[5,2],
[3,6]])
I could create a diagonal matrix from the diag
variable, diag_fill
the mat
matrix to 0
and add the results. However, this seems to use a bunch of unnecessary resources if I'm trying to alter the diagonal in place.
How do I set the diagonal of a matrix in numpy
?
Note: this is NOT a duplicate of this question, as they want to set the diagonal to a constant.