[Numpy-discussion] degree matrix construction

Charles R Harris charlesr.harris at gmail.com
Fri Sep 15 08:40:45 CDT 2006


On 9/15/06, Satya Upadhya <satyaupadhya at yahoo.co.in> wrote:
>
> Dear Friends,
> my question is the following:
>
> Suppose i have the following code:
>
> >>> from LinearAlgebra import *
>
> >>> from Numeric import *
> >>> A = [1,2,1,3,1,3,4,1,2]
> >>> B = reshape(A,(3,3))
> >>> C = sum(B,1)
> >>> C
> array([4, 7, 7])
> >>>
>
> Now, my problem is to construct a degree matrix D which is a 3 * 3 matrix
> with diagonal elements 4,7,7 (obtained from the elements of C) and all
> off-diagonal elements equal to 0.
>

Is this what you want to do?

In [2]: a = array([4, 7, 7])

In [3]: diagflat(a)
Out[3]:
array([[4, 0, 0],
       [0, 7, 0],
       [0, 0, 7]])

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20060915/f43e626c/attachment-0001.html 


More information about the Numpy-discussion mailing list