[Numpy-discussion] indexing question
Mon Jun 21 19:03:05 CDT 2010
On Mon, Jun 21, 2010 at 7:10 PM, Robert Kern <firstname.lastname@example.org> wrote:
> On Mon, Jun 21, 2010 at 17:42, Neal Becker <email@example.com> wrote:
>> Robert Kern wrote:
>>> On Mon, Jun 21, 2010 at 14:01, Neal Becker <firstname.lastname@example.org> wrote:
>>>> Can I find an efficient way to do this?
>>>> I have a 2d array, A, 80 rows by 880 columns.
>>>> I have a vector, B, of length 80, with scalar indexes.
>>> I assume you mean 880.
>>>> I want a vector output C where
>>>> C[i] = A[b[i],i] (i=0,879)
>>> C = A[b, np.arange(880)]
>> Thanks! Just what I needed.
>> I wouldn't have guessed this. Do we have a wiki to save useful examples
>> like this?
> This kind of indexing is documented here:
> Various examples are on the wiki here:
> Robert Kern
> "I have come to believe that the whole world is an enigma, a harmless
> enigma that is made terrible by our own mad attempt to interpret it as
> though it had an underlying truth."
> -- Umberto Eco
> NumPy-Discussion mailing list
And to be completely pedantic one could use ndarray.take to do this
about 3-4x faster (but less intuitively):
A.take(b * 880 + np.arange(880))
More information about the NumPy-Discussion