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

Home » Public Forums » archive » Sorting a matrix
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: Sorting a matrix [message #83475 is a reply to message #83346] Fri, 08 March 2013 14:24 Go to previous messageGo to previous message
cgguido is currently offline  cgguido
Messages: 195
Registered: August 2005
Senior Member
I was curious to compare this method with a more straightforward way. multisort.pro below does both.
Jeremy's method is twice as fast for large arrays. However, the two methods only give the same result for smallish arrays.

Any ideas what's going on?

;;;;;;;;;;;;;; EXAMPLES ;;;;;;;;;;;;;
multisort, round(randomu(s,20,1e1)*10)
; 1
; 0.00027489662 0.00026106834

multisort, round(randomu(s,20,1e2)*10)
; 0
; 0.00072312355 0.00039005280


;;;;;;;;;;;;;; CODE ;;;;;;;;;;;;;;;
PRO multisort, matrix

compile_opt idl2


matrixshape = size(matrix, /dimen)

t1 = systime(1)


; this gives you the range of each column:
matrixord = lonarr(matrixshape)
for i=0l,matrixshape[0]-1 do matrixord[i,*] = ord(matrix[i,*])
ordmax = max(matrixord, dimen=2)

; what do you need to multiply by to get a unique range?
column_multiply = [reverse(product(reverse(ordmax[1:*]+1), /int, /cumul)), 1]

; create a unique key and sort on it
sortkey = total(matrixord * rebin(column_multiply,matrixshape, /sample), /int, 1)
newmatrix = matrix[*, sort(sortkey)]
t2 = systime(1)


newmatrix2 = matrix
FOR i = matrixshape[0]-1, 0, -1 DO BEGIN
s = bsort(newmatrix2[i, *]);use bsort which maintains order of identical elements
newmatrix2 = newmatrix2[*, s]
ENDFOR
t3 = systime(1)

print,array_equal( newmatrix, newmatrix2)

print, t3-t2, t2-t1


RETURN
END
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: where, or, and loops. There has to be a better way.
Next Topic: SPAWN with AWK

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

Current Time: Sun Oct 12 15:12:46 PDT 2025

Total time taken to generate the page: 0.48167 seconds