[SciPy-user] Transforming 1-d array to 2-d array

Dave novin01@gmail....
Mon Feb 26 05:15:43 CST 2007


Rich Shepard <rshepard <at> appl-ecosys.com> writes:

> 
> On Sun, 25 Feb 2007, Rich Shepard wrote:
> 
> >   It appears that the eye() function is the tool, but when I try
> >
> > 	foo = eye(barEco,8,8,1)
> > 	print foo
> 
>    I've also tried triu() and mat(), but neither prints the results I need.
> 
> Rich
> 

eye simply creates an array with ones on the diagonal. To solve this problem I
would create a zero array of the correct size and then index into the array.

#Define array size
N = 8

#Define the data to put in the upper-diagonal part of the array
myData = rand(N*(N-1)/2)

#Create an index to the upper-diagonal part
idx = triu(ones([8,8])-eye(8)).nonzero()

#Instantiate a zero array and populate the upper-triangular part with myData
A = zeros([N,N],dtype=float)
A[idx] = myData

#Place ones on the diagonal
A += eye(N)






More information about the SciPy-user mailing list