[SciPy-user] Transforming 1-d array to 2-d array
Dave
novin01@gmail....
Wed Feb 28 08:13:27 CST 2007
Rich Shepard <rshepard <at> appl-ecosys.com> writes:
>
> On Mon, 26 Feb 2007, Rich Shepard wrote:
>
> > Now I need a bit more guidance, to complete the symmetrical matrices.
>
> It occurred to me after I posted this message that I'm going along the
> wrong path. I don't need a lower index, just the lower triangular half of
> the array with zeros along the principal diagonal and the upper half. Then
> I can add them (cell-wise, not matrix addition) and get the filled array I
> need.
>
> Rich
>
Sorry, have been quite busy....
The double brackets was the cause of your original error I suspect - hence
taking [0] fixed the problem.
I'm not sure if your data is in an array but if not you can cast it as an
array as follows:
barEco = asarray(barEco).squeeze()
The .squeeze() method will get rid of any extraneous brackets (singleton
dimensions)
I'm not too sure what you're now trying to achieve, but hopefully the code
below will help you understang array indexing.
-Dave
from numpy import asarray, ones, tril, triu, zeros
from numpy.random import rand
N = 5
upperData = (10.0*rand(N*(N-1)/2)).round() #dummy data
lowerData = (10.0*rand(N*(N-1)/2)).round() #dummy data
# Index into the upper half of an NxN array
upper_idx = triu(ones([N,N])-eye(N)).nonzero()
# Index into the upper half of an NxN array
lower_idx = tril(ones([N,N])-eye(N)).nonzero()
# Index into the diagonal elements of an NxN array
diag_idx = eye(N).nonzero()
C = zeros([N,N],dtype=float)
print C
C[upper_idx] = upperData
print C
C[lower_idx] = lowerData
print C
C += eye(N)
print C
A = 2.0*ones([N,N],dtype=float)
print A
B = 3.0*ones([N,N],dtype=float)
print B
print triu(A,k=1)
print tril(B,k=-1)
print triu(A,k=1) + tril(B,k=-1)
print triu(A,k=1) + tril(B,k=-1) + eye(N)
More information about the SciPy-user
mailing list