[SciPy-user] assign to diagonal values?

Alan G Isaac aisaac@american....
Wed Jan 30 21:08:01 CST 2008

Try the function below (from pyGAUSS).
Alan Isaac

#diagrv: insert v as diagonal of matrix x (2D only!)
def diagrv(x,v,copy=True):
	assert(len(x.shape)==2), "For 2-d arrays only."
	x = numpy.matrix( x, copy=copy )
	stride = 1 + x.shape[1]
	x.flat[ slice(0,None,stride) ] = v
	return x

More information about the SciPy-user mailing list