![](https://secure.gravatar.com/avatar/b684c02bab6c8d54c0c25c4b69ee1135.jpg?s=120&d=mm&r=g)
Jan. 31, 2008
3:44 a.m.
On 30-Jan-08, at 10:08 PM, Alan G Isaac wrote:
#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
Ooh that is clever. This should really go on the Cookbook page. Perhaps more generally, for in place modification: def setdiag(m, d): assert(len(x.shape) == 2) stride = 1 + x.shape[1] m.flat[slice(0,None,stride)] = d Regards, David