[Numpy-discussion] How do I make a diagonal matrix?

Travis Oliphant oliphant at ee.byu.edu
Fri Jun 23 12:11:38 CDT 2006


Alan G Isaac wrote:

>On Fri, 23 Jun 2006, Keith Goodman apparently wrote: 
>  
>
>>my x is a Nx1 matrix. I can't get it to work with matrices. 
>>    
>>
>
>Hmm. One would think that diag() would accept a flatiter 
>object, but it does not.  Shouldn't it??
>  
>

It doesn't?  

try:

a = rand(3,4)
diag(a.flat).shape

which prints
(12,12)
for me.

Also:

 >>> a = ones((2,3))

 >>> diag(a.flat)
array([[1, 0, 0, 0, 0, 0],
       [0, 1, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0],
       [0, 0, 0, 1, 0, 0],
       [0, 0, 0, 0, 1, 0],
       [0, 0, 0, 0, 0, 1]])







More information about the Numpy-discussion mailing list