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

Rich Shepard rshepard@appl-ecosys....
Mon Feb 26 15:21:46 CST 2007


On Mon, 26 Feb 2007, Dave wrote:

> #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)

   Wow! It certainly does what I need. If only I understood why ... and how
to create these solutions myself.

   I've read the brief descriptions of eye() and triu() in the book; where
can I read how to apply them to solutions like this?

Many thanks, Dave,

Rich

-- 
Richard B. Shepard, Ph.D.               |    The Environmental Permitting
Applied Ecosystem Services, Inc.        |          Accelerator(TM)
<http://www.appl-ecosys.com>     Voice: 503-667-4517      Fax: 503-667-8863


More information about the SciPy-user mailing list