[Numpy-discussion] array indexing question
Scott Sinclair
scott.sinclair.za@gmail....
Thu Nov 13 23:58:54 CST 2008
2008/11/14 Catherine Moroney <Catherine.M.Moroney@jpl.nasa.gov>:
> I have three arrays, with dimensions:
>
> A[np]
> L[np]
> S[np]
>
> where L and S indicate the line, smp co-ordinates for each of the
> "np" rows.
> I want to reconstruct the contents of [A] as a 2-dimensional matrix.
>
> The brain-dead version of what I want is:
>
> results = numpy.zeros(some_size, some_other_size)
> for idata in xrange(0,np):
> results[L[idata], S[idata]] = A[idata]
>
You can easily avoid the loop. Does this do what you want?
>>> L = np.array([0, 2, 4])
>>> S = np.array([0, 1, 3])
>>> A = np.array([1.3, 2.3, 3.3])
>>> result = np.zeros((5, 5))
>>> result
array([[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0.]])
>>> result[L, S] = A
>>> result
array([[ 1.3, 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. ],
[ 0. , 2.3, 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 3.3, 0. ]])
Cheers,
Scott
More information about the Numpy-discussion
mailing list