[Numpy-discussion] np.choose() question

Gökhan Sever gokhansever at gmail.com
Tue Jun 8 13:09:26 EDT 2010


On Tue, Jun 8, 2010 at 11:24 AM, Andreas Hilboll <lists at hilboll.de> wrote:

> Hi there,
>
> I have a problem, which I'm sure can somehow be solved using np.choose()
> - but I cannot figure out how :(
>
> I have an array idx, which holds int values and has a 2d shape. All
> values inside idx are 0 <= idx < n. And I have a second array times,
> which is 1d, with times.shape = (n,).
>
> Out of these two arrays I now want to create a 2d array having the same
> shape as idx, and holding the values contained in times, as indexed by
> idx.
>
> A simple np.choose(idx,times) does not work (error "Need between 2 and
> (32) array objects (inclusive).").
>
> Example:
>
> idx = [[4,2],[3,1]]
> times = [100,101,102,103,104]
>
>  From these two I want to create an array
>
> result = [[104,102],[103,101]]
>
> How can this be done?
>
> Thanks a lot for your insight!
>
> Andreas
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>

Here is a non numpy.choose solution:

newtimes = [times[idx[x][y]] for x in range(2) for y in range(2)]
np.array(newtimes).reshape(2,2)
array([[104, 102],
           [103, 101]])


-- 
Gökhan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20100608/aff91389/attachment.html>


More information about the NumPy-Discussion mailing list