[Numpy-discussion] Extracting required indices from the array of tuples

Simen Langseth simlangen at gmail.com
Fri Jan 2 07:39:18 EST 2015


Dear Jaime:

Thank you so much. Your codes are always great.

By the way, I have been waiting for several hours to get satisfactory
answer at:

http://codereview.stackexchange.com/questions/75457/faster-way-of-using-interp1d-in-2d-array?noredirect=1#comment137329_75457
http://stackoverflow.com/questions/27735832/faster-way-of-using-interp1d-in-2d-array

Please help me there if you have time.

Simen

On Fri, Jan 2, 2015 at 9:29 PM, Jaime Fernández del Río <
jaime.frio at gmail.com> wrote:

> On Fri, Jan 2, 2015 at 3:06 AM, Simen Langseth <simlangen at gmail.com>
> wrote:
>
>> import numpy as np
>> from scipy import signal
>>
>> y = np.array([[2, 1, 2, 3, 2, 0, 1, 0],
>>              [2, 1, 2, 3, 2, 0, 1, 0]])
>>
>> maximas = signal.argrelmax(y, axis=1)
>>
>> print maximas
>>
>> (array([0, 0, 1, 1], dtype=int64), array([3, 6, 3, 6], dtype=int64))
>>
>>
>> I want to extract only the first maxima of both rows, i.e., [3, 3] using
>> the tuples (maximas). How would you do it?
>>
>> Something like this should work:
>
> >>> rows, cols = maximas
> >>> first_in_row = np.concatenate(([True], rows[:-1] != rows[1:]))
> >>> rows = rows[first_in_row]
> >>> cols = cols[first_in_row]
> >>> y[rows, cols]
> array([3, 3])
>
> Jaime
>
> --
> (\__/)
> ( O.o)
> ( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes
> de dominación mundial.
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20150102/e48f6d83/attachment.html>


More information about the NumPy-Discussion mailing list