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

josef.pktd at gmail.com josef.pktd at gmail.com
Thu Jun 4 11:29:56 EDT 2009

```On Thu, Jun 4, 2009 at 11:19 AM, Alan G Isaac <aisaac at american.edu> wrote:
> On 6/4/2009 10:50 AM josef.pktd at gmail.com apparently wrote:
>> intersect1d gives set intersection if both arrays have
>> only unique elements (i.e. are sets).  I thought the
>> naming is pretty clear:
>
>> intersect1d(a,b)   set intersection if a and b with unique elements
>> intersect1d_nu(a,b)   set intersection if a and b with non-unique elements
>> setmember1d(a,b)  boolean index array for a of set intersection if a
>> and b with unique elements
>> setmember1d_nu(a,b)  boolean index array for a of set intersection if
>> a and b with non-unique elements
>
>
>>>> a
> array([1, 1, 2, 3, 3, 4])
>>>> b
> array([1, 4, 4, 4])
>>>> np.intersect1d_nu(a,b)
> array([1, 4])
>
> That is, intersect1d_nu is the actual set intersection
> function.  (I.e., intersect1d and intersect1d_nu would most
> naturally have swapped names.)  That is why the appended _nu
> will not communicate what was intended.  (I.e.,
> setmember1d_nu will not be a match for intersect1d_nu.)

intersect1d  is the intersection between sets (which are stored as
arrays), just like in the mathematical definition the two sets only
have unique elements

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

so  postfix `_nu` only means that this function also works if the two
arrays are not really sets, i.e. are not required to have unique
elements to make sense.

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

> Cheers,
> Alan Isaac

```

More information about the NumPy-Discussion mailing list