comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: How to get matching elements of array efficiently
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: How to get matching elements of array efficiently [message #69926 is a reply to message #69921] Wed, 24 February 2010 06:46 Go to previous messageGo to previous message
Jeremy Bailin is currently offline  Jeremy Bailin
Messages: 618
Registered: April 2008
Senior Member
On Feb 23, 3:36 pm, pp <pp.pente...@gmail.com> wrote:
> On Feb 23, 5:11 pm, JJ <j...@cornell.edu> wrote:
>
>> I'm looking for an efficient (non-loop) solution to this problem.  I
>> have two arrays A, B.  The elements of A are not necessarily unique,
>> nor is A necessarily sorted.  I'd like to find all the indices in A
>> that match values that occur in B.
>
>> For example, if A = [7,1,8,7,8], B = [7,8], the result should be
>> [0,2,3,4].
>
>> A and B can have many (order millions) of unique values, so I'd rather
>> avoid a loop if I can.
>
>> Is there any way to do this efficiently?
>
> Are your values integers as in the example? If they are, and if there
> are no big gaps in B (the number of elements of B is not much smaller
> than max(B)-min(B)), histogram comes to mind:
>
> h=histogram(A,binsize=1,min=min(b),max=max(b),reverse_indice s=ri)
> if (max(h) gt 0L) then res=ri[n_elements(h)+1:*] else (deal with the
> case of none found in b)
>
> Which would give the result you want in res, but ordered by bin (their
> order in B). In this example, res would be [0,3,2,4].

Of course, I'll have to pipe in here and say that if your values
aren't integers, or if there are large gaps in them, you can use UNIQ
and VALUE_LOCATE to map them into a set of consecutive integers, and
then use HIstOGRAM. :-)=

-Jeremy.
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Intersection of polyline curve and polygon in 3D
Next Topic: Deconvolving a set of points

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Fri Oct 10 03:46:21 PDT 2025

Total time taken to generate the page: 1.75749 seconds