Select values from one array that match multiple values from an other array [message #82218] |
Fri, 30 November 2012 07:15  |
PMagdon
Messages: 2 Registered: November 2012
|
Junior Member |
|
|
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]
So far I tested
C=WHERE(A EQ B) but this doesn't work.
Any ideas how to solve that without looping over B?
|
|
|
|
|
Re: Select values from one array that match multiple values from an other array [message #82468 is a reply to message #82218] |
Thu, 13 December 2012 16:24  |
liamsteele
Messages: 4 Registered: October 2012
|
Junior Member |
|
|
On Thursday, 13 December 2012 22:05:35 UTC, David Fanning wrote:
> ljs15@fsmail.net writes:
>
>
>
>> Just noticed that since I removed the indices stuff, the 'REVERSE_INDICES=rb' bit isn't needed anymore. I knew there'd be something wrong! :)
>
>
>
> OK, and you didn't give yourself credit in the Modification History,
>
> but I'll fix that for you. :-)
>
>
>
> Good job, though. Thanks!
>
>
>
> 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.")
No problem. I've actually learned something, so it was very worthwhile! I'm trying to think of all the things I'll be able to use the histogram function for now! :)
Liam
|
|
|
|
Re: Select values from one array that match multiple values from an other array [message #82470 is a reply to message #82218] |
Thu, 13 December 2012 13:55  |
Sir Loin Steak
Messages: 42 Registered: January 2012
|
Member |
|
|
On Thursday, December 13, 2012 9:51:38 PM UTC, lj...@fsmail.net wrote:
> On Thursday, December 13, 2012 7:51:56 PM UTC, lj...@fsmail.net wrote:
>
>> On Thursday, December 13, 2012 6:45:01 PM UTC, David Fanning wrote:
>
>>
>
>>> David Fanning writes:
>
>>
>
>>>
>
>>
>
>>>
>
>>
>
>>>
>
>>
>
>>>> Do you want a POSITIONS keyword for the SetDifference function?
>
>>
>
>>>
>
>>
>
>>>> If so, I think I'll leave that as an exercise for the reader.
>
>>
>
>>>
>
>>
>
>>>> No reason why I should be the only one working around here! :-)
>
>>
>
>>>
>
>>
>
>>>
>
>>
>
>>>
>
>>
>
>>> Oh, sorry. I see you *did* say SetDifference. I read it
>
>>
>
>>>
>
>>
>
>>> as SetIntersection. Duh!
>
>>
>
>>>
>
>>
>
>>>
>
>>
>
>>>
>
>>
>
>>> Well, I'll still leave it as an exercise for the reader,
>
>>
>
>>>
>
>>
>
>>> since I've already demonstrated how it can be done. It
>
>>
>
>>>
>
>>
>
>>> will probably take less than a minute. Send it to me
>
>>
>
>>>
>
>>
>
>>> when you are finished modifying it, and I'll publish
>
>>
>
>>>
>
>>
>
>>> it for you. :-)
>
>>
>
>>>
>
>>
>
>>>
>
>>
>
>>>
>
>>
>
>>> 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.")
>
>>
>
>>
>
>>
>
>>
>
>>
>
>> Ha - fair enough! I'll have a look over it and see what needs doing. :)
>
>
>
>
>
> Right - I think I've got it sorted. I've added position and count keywords. I spent ages on the indices stuff, then realised I didn't need them as I only wanted to know the elements in one array. Not to worry though - I've always steered clear of the histogram function as it looks horrible, but looking through the routine made me understand it a lot better. So, it wasn't time wasted!
>
>
>
> Anyhow, in case anyone wants a quick check the link to the file is below. I'm bound to have done something wrong!
>
>
>
> http://www.physics.open.ac.uk/~lsteele/setdifference.pro
>
>
>
> Cheers,
>
>
>
> Liam Steele
Just noticed that since I removed the indices stuff, the 'REVERSE_INDICES=rb' bit isn't needed anymore. I knew there'd be something wrong! :)
|
|
|
Re: Select values from one array that match multiple values from an other array [message #82471 is a reply to message #82218] |
Thu, 13 December 2012 13:51  |
Sir Loin Steak
Messages: 42 Registered: January 2012
|
Member |
|
|
On Thursday, December 13, 2012 7:51:56 PM UTC, lj...@fsmail.net wrote:
> On Thursday, December 13, 2012 6:45:01 PM UTC, David Fanning wrote:
>
>> David Fanning writes:
>
>>
>
>>
>
>>
>
>>> Do you want a POSITIONS keyword for the SetDifference function?
>
>>
>
>>> If so, I think I'll leave that as an exercise for the reader.
>
>>
>
>>> No reason why I should be the only one working around here! :-)
>
>>
>
>>
>
>>
>
>> Oh, sorry. I see you *did* say SetDifference. I read it
>
>>
>
>> as SetIntersection. Duh!
>
>>
>
>>
>
>>
>
>> Well, I'll still leave it as an exercise for the reader,
>
>>
>
>> since I've already demonstrated how it can be done. It
>
>>
>
>> will probably take less than a minute. Send it to me
>
>>
>
>> when you are finished modifying it, and I'll publish
>
>>
>
>> it for you. :-)
>
>>
>
>>
>
>>
>
>> 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.")
>
>
>
>
>
> Ha - fair enough! I'll have a look over it and see what needs doing. :)
Right - I think I've got it sorted. I've added position and count keywords. I spent ages on the indices stuff, then realised I didn't need them as I only wanted to know the elements in one array. Not to worry though - I've always steered clear of the histogram function as it looks horrible, but looking through the routine made me understand it a lot better. So, it wasn't time wasted!
Anyhow, in case anyone wants a quick check the link to the file is below. I'm bound to have done something wrong!
http://www.physics.open.ac.uk/~lsteele/setdifference.pro
Cheers,
Liam Steele
|
|
|
Re: Select values from one array that match multiple values from an other array [message #82474 is a reply to message #82218] |
Thu, 13 December 2012 11:51  |
Sir Loin Steak
Messages: 42 Registered: January 2012
|
Member |
|
|
On Thursday, December 13, 2012 6:45:01 PM UTC, David Fanning wrote:
> David Fanning writes:
>
>
>
>> Do you want a POSITIONS keyword for the SetDifference function?
>
>> If so, I think I'll leave that as an exercise for the reader.
>
>> No reason why I should be the only one working around here! :-)
>
>
>
> Oh, sorry. I see you *did* say SetDifference. I read it
>
> as SetIntersection. Duh!
>
>
>
> Well, I'll still leave it as an exercise for the reader,
>
> since I've already demonstrated how it can be done. It
>
> will probably take less than a minute. Send it to me
>
> when you are finished modifying it, and I'll publish
>
> it for you. :-)
>
>
>
> 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.")
Ha - fair enough! I'll have a look over it and see what needs doing. :)
|
|
|
Re: Select values from one array that match multiple values from an other array [message #82477 is a reply to message #82218] |
Thu, 13 December 2012 10:45  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
David Fanning writes:
> Do you want a POSITIONS keyword for the SetDifference function?
> If so, I think I'll leave that as an exercise for the reader.
> No reason why I should be the only one working around here! :-)
Oh, sorry. I see you *did* say SetDifference. I read it
as SetIntersection. Duh!
Well, I'll still leave it as an exercise for the reader,
since I've already demonstrated how it can be done. It
will probably take less than a minute. Send it to me
when you are finished modifying it, and I'll publish
it for you. :-)
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.")
|
|
|
Re: Select values from one array that match multiple values from an other array [message #82478 is a reply to message #82218] |
Thu, 13 December 2012 10:39  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
ljs15@fsmail.net writes:
> I was just about to post a question about this very topic - spooky! Would it also be possible to amend the SetDifference code so it also records positions, as I would like to know which elements of one array have no match in a different array.
I understand "spooky", but I don't understand
the rest of this question.
Do you want a POSITIONS keyword for the SetDifference function?
If so, I think I'll leave that as an exercise for the reader.
No reason why I should be the only one working around here! :-)
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.")
|
|
|
Re: Select values from one array that match multiple values from an other array [message #82479 is a reply to message #82218] |
Thu, 13 December 2012 10:24  |
Sir Loin Steak
Messages: 42 Registered: January 2012
|
Member |
|
|
On Thursday, December 13, 2012 4:35:28 PM UTC, David Fanning wrote:
> 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.")
I was just about to post a question about this very topic - spooky! Would it also be possible to amend the SetDifference code so it also records positions, as I would like to know which elements of one array have no match in a different array.
Many thanks
|
|
|