[SciPy-user] intersect (matlab)

Stéfan van der Walt stefan at sun.ac.za
Mon Feb 16 02:59:46 EST 2009


2009/2/16 Bernardo M. Rocha <bernardo.rocha at 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



More information about the SciPy-User mailing list