[SciPy-user] assign to diagonal values?

Carlos Scheidegger cscheid@sci.utah....
Wed Jan 30 19:47:17 CST 2008

Say I have a matrix m whose diagonal values I want to replace with the
contents of vector new_diagonal. Right now, I'm using

m -= diag(m)
m += diag(new_diagonal)

but that seems pretty wasteful (especially if diag() creates new arrays, which
I suspect it does). Is there a standard (or any efficient) way to assign the
diagonal values directly without an explicit for loop? Googling for
"site:www.scipy.org assign diagonal" only returned results for sparse
matrices, which is not the case here - these are plain numpy arrays.

Thanks in advance,

More information about the SciPy-user mailing list