[SciPy-user] assign to diagonal values?

Carlos Scheidegger cscheid at sci.utah.edu
Wed Jan 30 20:47:17 EST 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,
-carlos



More information about the SciPy-User mailing list