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

Rich Shepard rshepard at appl-ecosys.com
Mon Feb 26 15:43:37 EST 2007


On Mon, 26 Feb 2007, Dave wrote:

> 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.

Hi, Dave,

   What, then, is the difference between eye() and identity()?

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

   Well, rather than random, these data are the averages of averages of each
item in the list of tuples retrieved from the database table.

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

   I don't immediately understand what this is doing. I'll have to play with
it in ipython.

   Ultimately, I need the averaged item values in the upper half of an 8x8
matrix, 1s along the diagonal, and 1/values in the lower half of the matrix.

   Probably not the simplest introduction to NymPy and SciPy, but it's what I
need to do.

Many thanks,

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