From: David D. <dav...@lo...> - 2006-06-23 16:09:09
|
On Fri, Jun 23, 2006 at 07:55:47AM -0700, Keith Goodman wrote: > On 6/23/06, Sven Schreiber <sve...@gm...> wrote: > > Keith Goodman schrieb: > > > How do I make a NxN diagonal matrix with a Nx1 column vector x along > > > the diagonal? > > > > > > > >>> help(n.diag) > > Help on function diag in module numpy.lib.twodim_base: > > > > diag(v, k=3D0) > > returns the k-th diagonal if v is a array or returns a array > > with v as the k-th diagonal if v is a vector. >=20 > I tried >=20 > >> x =3D rand(3,1) >=20 > >> diag(x) > array([ 0.87113114]) >=20 > Isn't rand(3,1) a vector? No: In [13]: rand(3).shape =20 Out[13]: (3,) In [14]: rand(3,1).shape Out[14]: (3, 1) A "vector" is an array with only one dimension. Here, you have a 3x1 "matrix"... >=20 > Off list I was given the example: > x=3Drand(3) > diag(3) So you've got the solution! > That works. But my x is a Nx1 matrix. I can't get it to work with matrice= s. ??? Don't understand what you cannot make work, here. In [15]: x=3Drand(3,1) =20 In [18]: diag(x[:,0]) Out[18]:=20 array([[ 0.2287158 , 0. , 0. ], [ 0. , 0.50571537, 0. ], [ 0. , 0. , 0.72304857]]) What else would you like? David > Joris: The Numpy Example List looks good. I hadn't come across that befor= e. >=20 David Douard LOGILAB, Paris (France) Formations Python, Zope, Plone, Debian : http://www.logilab.fr/formations D=E9veloppement logiciel sur mesure : http://www.logilab.fr/services Informatique scientifique : http://www.logilab.fr/science |