Re: Sorting (big) int array: Eliminate for loop [message #68047] |
Tue, 22 September 2009 05:15 |
Luds
Messages: 7 Registered: September 2009
|
Junior Member |
|
|
On Sep 21, 5:37 pm, greg <greg.a...@googlemail.com> wrote:
> How about this...
>
> pro test
> a=['I','E','F','D','A','J','C','B','G','H']
> b=['E','B','I','J','D','A','C','H','F','G']
>
> qa=sort(a)
> qb=sort(b)
> qa2=sort(qa)
> elements=qb[qa2]
>
> print,b[elements]
> end
>
> IDL> test
> I E F D A J C B G H
>
> Sortingthe sorted indices is the trick. I think it's right, but it
> makes my head hurt anyway.
>
> regards,
> Greg
Perfect, thanks. This works great. Having to do a couple sorts doesn't
add too much overhead to my code. It's much faster now.
By the looks of it the value_locate function could do the trick too,
but it seems to want one of the arrays sorted anyway, which isn't the
case for me.
Thanks for the advice guys. Very helpful.
A
|
|
|
Re: Sorting (big) int array: Eliminate for loop [message #68059 is a reply to message #68047] |
Mon, 21 September 2009 08:37  |
greg.addr
Messages: 160 Registered: May 2007
|
Senior Member |
|
|
How about this...
pro test
a=['I','E','F','D','A','J','C','B','G','H']
b=['E','B','I','J','D','A','C','H','F','G']
qa=sort(a)
qb=sort(b)
qa2=sort(qa)
elements=qb[qa2]
print,b[elements]
end
IDL> test
I E F D A J C B G H
Sorting the sorted indices is the trick. I think it's right, but it
makes my head hurt anyway.
regards,
Greg
|
|
|
Re: Sorting (big) int array: Eliminate for loop [message #68060 is a reply to message #68059] |
Mon, 21 September 2009 08:29  |
Brian Larsen
Messages: 270 Registered: June 2006
|
Senior Member |
|
|
I think this is a use for value_locate. But there are lots of
caveats. Like A would have to be sorted which is possibly a speed
hit. Are all the numbers unique? If not then
B[elements]=A could have more than one answer, which could be bad.
Read through the value_locate help and see if it can work for your
application.
Cheers,
Brian
------------------------------------------------------------ --------------
Brian Larsen
Boston University
Center for Space Physics
|
|
|