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