Finding Positions of Identical Entries in Arrays of Different Size
Dear All, A problem which should be rather easy, but which is giving me a hard time. (1) Consider for instance the following two arrays A=[12,78,98,654,7665,1213,765] and B=[98,765, 654] Now, what I need is an efficient way of finding the positions of the entries of A equal to the entries of B, that is to say in the case of the two arrays above (counting from zero) C=[2,6,3]. Note that in the example there is a single entry of A equal to the i-th entry of B. I have tried using scipy.where, but unsuccessfully. Any ideas about how to code that efficiently? (2) I would like to extend this to the case where A and B are m times n arrays, e.g. consider the case A= 233 789 987 734 890 253 876 321 534 489 and B= 987 734 876 321 and C is now the 1D vector returning the positions of the rows of A equal to those of B, i.e. C=[1, 3]. Any suggestions is really welcome. Kind Regards Lorenzo
On Thu, Aug 27, 2009 at 3:31 PM, Lorenzo Isella<lorenzo.isella@gmail.com> wrote:
Dear All, A problem which should be rather easy, but which is giving me a hard time. (1) Consider for instance the following two arrays
A=[12,78,98,654,7665,1213,765] and B=[98,765, 654]
np.where(np.setmember1d(A,B))
Now, what I need is an efficient way of finding the positions of the entries of A equal to the entries of B, that is to say in the case of the two arrays above (counting from zero)
C=[2,6,3]. Note that in the example there is a single entry of A equal to the i-th entry of B. I have tried using scipy.where, but unsuccessfully. Any ideas about how to code that efficiently? (2) I would like to extend this to the case where A and B are m times n arrays, e.g. consider the case
A= 233 789 987 734 890 253 876 321 534 489
and
B= 987 734 876 321
and C is now the 1D vector returning the positions of the rows of A equal to those of B, i.e.
C=[1, 3].
This is probably not the best solution but it works in cases where the elements in B are not unique. qmax=max(B.max(),A.max()) Bp=B[:,0].copy() Ap=A[:,0].copy() Bp+=B[:,1]*qmax Ap+=A[:,1]*qmax C=np.where(np.setmember1d(A,B)) I hope this helps. Damian
Any suggestions is really welcome. Kind Regards
Lorenzo _______________________________________________ SciPy-User mailing list SciPy-User@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
-- ----------------------------------------------------- Damian Eads Ph.D. Candidate University of California Computer Science 1156 High Street Machine Learning Lab, E2-489 Santa Cruz, CA 95064 http://www.soe.ucsc.edu/~eads
On Thu, Aug 27, 2009 at 4:26 PM, Damian Eads<eads@soe.ucsc.edu> wrote:
On Thu, Aug 27, 2009 at 3:31 PM, Lorenzo Isella<lorenzo.isella@gmail.com> wrote:
Dear All, A problem which should be rather easy, but which is giving me a hard time. (1) Consider for instance the following two arrays
A=[12,78,98,654,7665,1213,765] and B=[98,765, 654]
np.where(np.setmember1d(A,B))
Now, what I need is an efficient way of finding the positions of the entries of A equal to the entries of B, that is to say in the case of the two arrays above (counting from zero)
C=[2,6,3]. Note that in the example there is a single entry of A equal to the i-th entry of B. I have tried using scipy.where, but unsuccessfully. Any ideas about how to code that efficiently? (2) I would like to extend this to the case where A and B are m times n arrays, e.g. consider the case
A= 233 789 987 734 890 253 876 321 534 489
and
B= 987 734 876 321
and C is now the 1D vector returning the positions of the rows of A equal to those of B, i.e.
C=[1, 3].
This is probably not the best solution but it works in cases where the elements in B are not unique.
qmax=max(B.max(),A.max()) Bp=B[:,0].copy() Ap=A[:,0].copy() Bp+=B[:,1]*qmax Ap+=A[:,1]*qmax C=np.where(np.setmember1d(A,B))
Err, I mean, C=np.where(np.setmember1d(Ap,Bp)) Damian
participants (2)
-
Damian Eads
-
Lorenzo Isella