Re: Select values from one array that match multiple values from an other array [message #82212] |
Fri, 30 November 2012 08:32  |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
Y
On Friday, November 30, 2012 10:15:28 AM UTC-5, PMagdon wrote:
> Dear all,
>
> I tried to use WHERE for multiple comparisons of two arrays.
>
> Here is a minimal example:
> 1.) Two arrays
> A=[1,3,2,5,2,6,1,9]
> B=[1,5]
> 2.) I need to extract all positions of A where any of the values from B occur in A. So that C should look like this:
> C=[0,3,6]
You can also try MATCH2 from the IDL astronomy library.
http://idlastro.gsfc.nasa.gov/ftp/pro/misc/match2.pro
This routine tells you which item in B matches A as well.
For example,
match2, a, b, suba
;; ---> [0L,-1L,-1L,1L,-1L,-1L,0L,-1L]
;; 0 1 2 3 4 5 6 7
indicates that A[0] matches B[0], A[3] matches B[1], and A[6] matches B[0]. You didn't actually ask for this level of information, but you can get what you want by using WHERE(SUBA GE 0).
Craig
|
|
|
|
|
Re: Select values from one array that match multiple values from an other array [message #82215 is a reply to message #82214] |
Fri, 30 November 2012 07:57   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
David Fanning writes:
> Whoops! You wanted positions. Nevermind. :-)
OK, I just added a POSITIONS keyword to SetIntersection
that will return the positions in vector A where the
values in vector B appear.
A=[1,3,2,5,2,6,1,9]
B=[1,5]
C = SetIntersection(a, b, Positions=pos)
Print, pos
0 6 3
You can find the new program here:
http://www.idlcoyote.com/programs/setintersection.pro
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
|
|
|
Re: Select values from one array that match multiple values from an other array [message #82216 is a reply to message #82215] |
Fri, 30 November 2012 07:36   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
David Fanning writes:
>
> PMagdon writes:
>
>> I tried to use WHERE for multiple comparisons of two arrays.
>>
>> Here is a minimal example:
>>
>> 1.) Two arrays
>>
>> A=[1,3,2,5,2,6,1,9]
>> B=[1,5]
>>
>> 2.) I need to extract all positions of A where any of the values from B occur in A. So that C should look like this:
>>
>> C=[0,3,6]
>>
>> So far I tested
>> C=WHERE(A EQ B) but this doesn't work.
>>
>> Any ideas how to solve that without looping over B?
>
> c = SetIntersection(a, b)
>
> http://www.idlcoyote.com/tips/set_operations.html
Whoops! You wanted positions. Nevermind. :-)
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
|
|
|
Re: Select values from one array that match multiple values from an other array [message #82217 is a reply to message #82216] |
Fri, 30 November 2012 07:31   |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
PMagdon writes:
> I tried to use WHERE for multiple comparisons of two arrays.
>
> Here is a minimal example:
>
> 1.) Two arrays
>
> A=[1,3,2,5,2,6,1,9]
> B=[1,5]
>
> 2.) I need to extract all positions of A where any of the values from B occur in A. So that C should look like this:
>
> C=[0,3,6]
>
> So far I tested
> C=WHERE(A EQ B) but this doesn't work.
>
> Any ideas how to solve that without looping over B?
c = SetIntersection(a, b)
http://www.idlcoyote.com/tips/set_operations.html
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
|
|
|
Re: Select values from one array that match multiple values from an other array [message #82480 is a reply to message #82215] |
Thu, 13 December 2012 08:35  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
David Fanning writes:
> OK, I just added a POSITIONS keyword to SetIntersection
> that will return the positions in vector A where the
> values in vector B appear.
>
> A=[1,3,2,5,2,6,1,9]
> B=[1,5]
> C = SetIntersection(a, b, Positions=pos)
> Print, pos
> 0 6 3
At Bob Stockwell's suggestion (and with his code!) I
have added the keywords INDICES_A and INDICES_B
to this program to allow you to return the matching
indices in BOTH vectors. Note that this assumes the
vectors A and B contain *unique* values. In other
words, INDICES_A and POSITIONS are identical vectors
IF vector A and B contain unique values. If they
don't, then POSITIONS will contain ALL of the locations
in vector A where the match occurs.
You can find the updated program here:
http://www.idlcoyote.com/programs/setintersection.pro
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
Sepore ma de ni thue. ("Perhaps thou speakest truth.")
|
|
|