[Numpy-discussion] finding index in an array...

josef.pktd at gmail.com josef.pktd at gmail.com
Sat Jun 13 06:37:00 EDT 2009


On Sat, Jun 13, 2009 at 5:51 AM, fred<fredmfp at gmail.com> wrote:
> Emmanuelle Gouillart a écrit :
>> Hi Fred,
> Hi Manue ;-)
>
>> here is another solution
>>>>> A = np.arange(99).reshape((33,3)
>>>>> mask = (A==np.array([0,1,2]))
>>>>> np.nonzero(np.prod(mask, axis=1))[0]
>> array([0]
>>
>> I found it to be less elegant than Josef's solution changing the dtype of
>> the array, but it may be easier to understand if you're not very familiar
>> with dtypes.
> I have no problem.

I think using a view instead of broadcasting in this case is
unnecessarily complicated. Maybe I was practicing views with
structured types too much.

using "all" instead of "prod" may be more descriptive:

>>> (A == np.array([1,2,3])).all(1).nonzero()[0]
array([3, 7])

>>> (A == [1,2,3]).all(1).nonzero()[0]
array([3, 7])

Josef

>
>> Also, it is a bit faster on my computer:
> I take it.
>
> Thanks!
>
>
> Cheers,
>
> --
> Fred
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



More information about the NumPy-Discussion mailing list