Question

I have a large array of ordered pairs in N-dimensions. I then have a single test array in N-dimensions, that I want to find all of the indices of its locations in the large array. A simple example looks like the following:

>>> import numpy as np
>>> x = np.array(  ((1,2),(3,4),(5,6)) )
>>> y = np.array( (1,2) )
>>> x == y
array([[ True,  True],
   [False, False],
   [False, False]], dtype=bool)

What I want, however, is:

array([True,
   False,
   False], dtype=bool)

Is this possible? I want to avoid looping over the entire large array and testing all individual objects to find the indices. There are multiple locations in the large array where each test array appears, and I need all of the indices.

Am I missing something simple?

Était-ce utile?

La solution

(x == y).all(axis=1)

That should do it. It tests whether all entries in each row of x == y are true and returns a 1D array of results. It's roughly equivalent to

numpy.array([all(vector) for vector in x == y])
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top