[SciPy-user] intersect (matlab)

Robert Kern robert.kern at gmail.com
Thu Feb 12 02:56:21 EST 2009


On Thu, Feb 12, 2009 at 01:23, Bernardo M. Rocha
<bernardo.rocha at meduni-graz.at> wrote:
> Hi Guys,
>
> Is there an equivalent in scipy/numpy to the following MATLAB code??? Or
> is there a way to do the same and get this ia and ib?
>
> A = [1 2 3 6]; B = [1 2 3 4 6 10 20];
> [c, ia, ib] = intersect(A, B);
> disp([c; ia; ib])     1     2     3     6
>    1     2     3     4
>    1     2     3     5

In [40]: A = array([1, 2, 3, 6])

In [41]: B = array([1,2,3,4,6,10,20])

In [42]: c = intersect1d(A, B)

In [43]: c
Out[43]: array([1, 2, 3, 6])

In [46]: ma = setmember1d(A, B)

In [47]: ma
Out[47]: array([ True,  True,  True,  True], dtype=bool)

In [48]: ia = nonzero(ma)[0]

In [49]: ia
Out[49]: array([0, 1, 2, 3])

In [50]: mb = setmember1d(B, A)

In [51]: mb
Out[51]: array([ True,  True,  True, False,  True, False, False], dtype=bool)

In [52]: ib = nonzero(mb)[0]

In [53]: ib
Out[53]: array([0, 1, 2, 4])

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco



More information about the SciPy-User mailing list