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

lorenzo bolla lbolla at gmail.com
Wed Mar 26 10:36:55 EDT 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 at 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 at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>



-- 
Lorenzo Bolla
lbolla at gmail.com
http://lorenzobolla.emurse.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20080326/0f176b75/attachment.html>


More information about the NumPy-Discussion mailing list