[Numpy-discussion] Quikest way to create a diagonal matrix ?

lorenzo bolla lbolla@gmail....
Wed Mar 26 09:36:55 CDT 2008


numpy.tri

In [31]: T = numpy.tri(m)

In [32]: z.T * T + z * T.T
Out[32]:
array([[  0.,   1.,   2.,   3.,   4.],
       [  1.,  12.,   7.,   8.,   9.],
       [  2.,   7.,  24.,  13.,  14.],
       [  3.,   8.,  13.,  36.,  19.],
       [  4.,   9.,  14.,  19.,  48.]])

hth,
L.

On Wed, Mar 26, 2008 at 2:48 PM, Pierre GM <pgmdevlist@gmail.com> wrote:

> All,
> What's the quickest way to create a diagonal matrix ? I already have the
> elements above the main diagonal. Of course, I could use loops:
> >>>m=5
> >>>z = numpy.arange(m*m).reshape(m,m)
> >>>for k in range(m):
> >>>    for j in range(k+1,m):
> >>>        z[j,k] = z[k,j]
> But I was looking for something more efficient.
> Thanks a lot in advance !
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>



-- 
Lorenzo Bolla
lbolla@gmail.com
http://lorenzobolla.emurse.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080326/0f176b75/attachment-0001.html 


More information about the Numpy-discussion mailing list