[Matrix-SIG] QUERY: Array indexing
Perry Stoll
pas@scansoft.com
Wed, 10 Mar 1999 18:29:17 -0800
Assuming you are using NumPy, the function you want is Numeric.take().
See:
http://starship.python.net/crew/da/numtut/array.html#SEC24
And from the incomparable Tim Peters, here's a lesson on gather/scatter in
general (sorry if this link gets broken up across two lines):
http://x9.dejanews.com/[ST_rn=ps]/getdoc.xp?AN=227662160&CONTEXT=921108267.1
22880047&hitnum=3:
which yields the following functions:
def gather(seq, indices):
from operator import __getitem__
return map(__getitem__, (seq,)*len(indices), indices)
def scatter(seq, indices,vals):
from operator import __setitem__
return map(__setitem__, (seq,)*len(indices), indices,vals)
Cheers,
Perry
-----Original Message-----
From: Janne Sinkkonen <janne@avocado.pc.helsinki.fi>
To: Stuart Feerick <smf26@cus.cam.ac.uk>
Cc: matrix-sig@python.org <matrix-sig@python.org>
Date: Wednesday, March 10, 1999 12:48 PM
Subject: Re: [Matrix-SIG] QUERY: Array indexing
>Stuart Feerick <smf26@cus.cam.ac.uk> writes:
>
>> I have an index of values, i=[1,2,3], and need to access element
>> [1,2,3] of the array, a=array([3,3,3]). How do I do this going through i?
>
>As far as I understand what you want, you can just write a[i]=5, for
>example, or z=a[i]. the variable i may be a sequence of indexes:
>
>a=zeros((2,3,4))
>for i in transpose(reshape(indices(a.shape),(len(a.shape),-1))): a[i]=1
>