[Numpy-discussion] Indexing by label in 3rd dimension

Warren Weckesser warren.weckesser@enthought....
Fri Sep 16 10:43:39 CDT 2011


On Fri, Sep 16, 2011 at 10:18 AM, Benjamin Landenberger <
benjamin.landenberger@imtek.uni-freiburg.de> wrote:

> Hello list!
>
> I have an array *mask* of shape (a, b) and another array *intensities*
> of shape (N, a, b), where the values in *mask* range from 0 to N-1. It
> is somehow similar to label arrays in scipy.ndimage.
>
> Now I want to pick those entries from the first dimension of
> *intensities* which are given by *mask*. The returned array shall again
> wave shape (a, b).
>
> Can this be done with fancy indexing?
>


The choose() function can be used:

 In [19]: intensities
Out[19]:
array([[[ 1,  2,  3],
        [ 4,  5,  6],
        [ 7,  8,  9]],

       [[11, 12, 13],
        [14, 15, 16],
        [17, 18, 19]],

       [[21, 22, 23],
        [24, 25, 26],
        [27, 28, 29]]])

In [20]: mask
Out[20]:
array([[0, 0, 1],
       [1, 1, 1],
       [1, 2, 2]])

In [21]: choose(mask, intensities)
Out[21]:
array([[ 1,  2, 13],
       [14, 15, 16],
       [17, 28, 29]])


Warren



> Thank you,
>
> Ben
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20110916/0ddb568a/attachment.html 


More information about the NumPy-Discussion mailing list