[Numpy-discussion] extract elements of an array that are contained in another array?

Alan G Isaac aisaac at american.edu
Thu Jun 4 12:32:33 EDT 2009

On 6/4/2009 11:29 AM josef.pktd at gmail.com apparently wrote:
> intersect1d  is the intersection between sets (which are stored as 
> arrays), just like in the mathematical definition the two sets only 
> have unique elements 

Hmmm. OK, I see you and Robert believe this.
But it does not match the documentation.
But indeed, I see that the documentation is incorrect.

>>> np.intersect1d([1,1,2,3,3,4],[1,4])
array([1, 1, 3, 4])

Is this a bug or a documentation bug?

> intersect1d_nu is the intersection between two arrays which can have 
> repeated elements. The result is a set, i.e. unique elements, stored 
> as an array 

> same for setmember1d, setmember1d_nu 

I cannot understand this.
Following your proposed reasoning,
I expect a[setmember1d_nu(a,b)]
to return the same as
intersect1d_nu(a, b).
It does not.

> so  postfix `_nu` only means that this function also works 
> if the two arrays are not really sets

But that just begs the question: what does 'works' mean?
See my previous comment (above).

> intersect1d should throw a domain error if you give it arrays with 
> non-unique elements, which is not done for speed reasons 

*If* intersect1d behaved *exactly* as documented,
the example
intersect1d(a, np.unique(b))
shows that the documented behavior can be useful.
And indeed, this would be the match to

Alan Isaac

More information about the NumPy-Discussion mailing list