[SciPy-user] intersect (matlab)

334 views
Skip to first unread message

Bernardo M. Rocha

unread,
Feb 12, 2009, 2:23:44 AM2/12/09
to scipy...@scipy.org
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


Best regards.
Bernardo M. Rocha
_______________________________________________
SciPy-user mailing list
SciPy...@scipy.org
http://projects.scipy.org/mailman/listinfo/scipy-user

Robert Kern

unread,
Feb 12, 2009, 2:56:21 AM2/12/09
to SciPy Users List
On Thu, Feb 12, 2009 at 01:23, Bernardo M. Rocha
<bernard...@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

Bernardo M. Rocha

unread,
Feb 13, 2009, 9:21:29 AM2/13/09
to scipy...@scipy.org
Hi Robert Kern,

thanks a lot for your help! Just another question, is there a way to do it in a matrix (that is, the intersection in the rows of 2 matrices)? The matlab version would be like:

[c,ia,ib] = intersect(A,B,'rows')

where A and B are matrices with the same number of columns.

Best regards,
Bernardo M. Rocha

Robert Kern

unread,
Feb 13, 2009, 6:34:52 PM2/13/09
to SciPy Users List
On Fri, Feb 13, 2009 at 08:21, Bernardo M. Rocha
<bernard...@meduni-graz.at> wrote:
> Hi Robert Kern,
>
> thanks a lot for your help! Just another question, is there a way to do it in a matrix (that is, the intersection in the rows of 2 matrices)? The matlab version would be like:
>
> [c,ia,ib] = intersect(A,B,'rows')
>
> where A and B are matrices with the same number of columns.

I don't know exactly what you mean, off-hand. Can you show me an example?

--
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

Bernardo M. Rocha

unread,
Feb 16, 2009, 1:53:40 AM2/16/09
to scipy...@scipy.org
Hi Robert Kern,

I have 2 matrices of dimensions npts1 x 3 and npts2 x 3, and I would
like to figure out the intersection between the rows of these matrices.
If you think that the matrices are lists of points with their
coordinates, I want to find out the common points to both lists. That's
it. In matlab you can simply do:

[c,iai,ib] = intersect(A,B,'rows')

But the intersect in python only works with 1D arrays.

Best regards,
Bernardo M. Rocha

Stéfan van der Walt

unread,
Feb 16, 2009, 2:59:46 AM2/16/09
to SciPy Users List
2009/2/16 Bernardo M. Rocha <bernard...@meduni-graz.at>:

> I have 2 matrices of dimensions npts1 x 3 and npts2 x 3, and I would
> like to figure out the intersection between the rows of these matrices.
> If you think that the matrices are lists of points with their
> coordinates, I want to find out the common points to both lists. That's
> it. In matlab you can simply do:
>
> [c,iai,ib] = intersect(A,B,'rows')
>
> But the intersect in python only works with 1D arrays.

Here's a place to start:

# Setup some dummy data
a = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15]])
b = np.array([[10, 11, 12], [1, 4, 5], [1, 2, 3], [13, 14, 15], [1, 2, 4]])

# Calculate the indices of the intersecting rows
intersection = np.logical_or.reduce(np.logical_and.reduce(a == b[:,
None], axis=2))

print a[intersection]

Regards
Stéfan

Robert Cimrman

unread,
Feb 16, 2009, 7:07:53 AM2/16/09
to SciPy Users List
Bernardo M. Rocha wrote:
> Hi Robert Kern,
>
> I have 2 matrices of dimensions npts1 x 3 and npts2 x 3, and I would
> like to figure out the intersection between the rows of these
> matrices. If you think that the matrices are lists of points with
> their coordinates, I want to find out the common points to both
> lists. That's it. In matlab you can simply do:
>
> [c,iai,ib] = intersect(A,B,'rows')
>
> But the intersect in python only works with 1D arrays.
>
> Best regards, Bernardo M. Rocha

These questions appear from time to time - it would be nice to add more
kwarg options to all the functions in the arraysetops module, like
return_index, rows (or better, axis!), etc. Unfortunately, I (the
culprit of arraysetops) am too swamped by work to look at it right now.
But it's on my TODO list.

Best regards,
r.

Reply all
Reply to author
Forward
0 new messages