Re: Interesting property of sort [message #53990] |
Wed, 16 May 2007 06:40  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
hradilv writes:
> To add the obvious(?) - be careful if you try this with repeated
> values.
>
> test = [5,4,4,1,3,2]
> sortindex = sort(test)
> lookup = sort(sortindex)
> print, lookup[4]
> ; prints "2" [hey what about "1"]...
Yeah, better use BSORT for this. :-)
http://www.dfanning.com/tips/sort.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 thui. ("Perhaps thou speakest truth.")
|
|
|
Re: Interesting property of sort [message #53991 is a reply to message #53990] |
Wed, 16 May 2007 06:35   |
Vince Hradil
Messages: 574 Registered: December 1999
|
Senior Member |
|
|
On May 16, 7:41 am, cmanc...@ufl.edu wrote:
> while attempting to build a faster star matching algorithm, I
> "discovered" an interesting property of sort - or, an interesting use
> for it. Of course, you guys have all probably seen this before
> anyway, but I thought I would share it because it can be very useful.
> Essentially, I found the easy way to make a sort lookup table. So,
> let's say you have the array:
>
> test = [5,4,1,3,2]
>
> and you sort it:
>
> sortindex = sort(test)
> sorted = test[sortindex]
>
> now, what if you want to know where the 5 (or any other number) ended
> up in the sorted array? It turns out that you can make a lookup table
> by simply sorting sortindex.
>
> lookup = sort(sortindex)
>
> so, if you wanted to know where the first element of test ended up in
> the sorted list, you would say:
>
> print,lookup[0]
> ; prints "4"
> print,sorted[lookup[0]]
> ; prints "5"
>
> In retrospect, I suppose this result isn't suprising, and probably
> should have been immediately obvious. Still, I thought I'd share.
To add the obvious(?) - be careful if you try this with repeated
values.
test = [5,4,4,1,3,2]
sortindex = sort(test)
lookup = sort(sortindex)
print, lookup[4]
; prints "2" [hey what about "1"]...
|
|
|
Re: Interesting property of sort [message #54089 is a reply to message #53990] |
Wed, 16 May 2007 06:44  |
cmancone
Messages: 30 Registered: May 2007
|
Member |
|
|
excellent point, thanks for the tip. Out of curiosity, does bsort add
a lot of overhead compared to a regular sort?
On May 16, 9:40 am, David Fanning <n...@dfanning.com> wrote:
> hradilv writes:
>> To add the obvious(?) - be careful if you try this with repeated
>> values.
>
>> test = [5,4,4,1,3,2]
>> sortindex = sort(test)
>> lookup = sort(sortindex)
>> print, lookup[4]
>> ; prints "2" [hey what about "1"]...
>
> Yeah, better use BSORT for this. :-)
>
> http://www.dfanning.com/tips/sort.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 thui. ("Perhaps thou speakest truth.")
|
|
|