[SciPy-User] Finding Positions of Identical Entries in Arrays of Different Size
Damian Eads
eads at soe.ucsc.edu
Thu Aug 27 19:26:51 EDT 2009
On Thu, Aug 27, 2009 at 3:31 PM, Lorenzo Isella<lorenzo.isella at 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 at 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
More information about the SciPy-User
mailing list