IDL 雙重排序函式

2022-06-09 08:39:13 字數 3415 閱讀 1330

;+

; :description:

; double sort

; :input:

;input array dimensions: 2*n

;idx: 0 - sort by the first column firstly (default)

;1 - sort by the second column firstly

;type: 0 - ascending sort both columns (default)

;1 - descending sort both columns

;2 - ascending sort firstly, and the descending sort

;3 - descending sort firstly, and then ascending sort

; :output:

;output sorted array: 2*n;

; :example:

; arr = [[9,4],[3,4],[3,1],[2,6],[9,6],[3,5],[5,4]]

;arrnew = double_sort(arr, 1, 3)

; :author: [email protected]

;-function double_sort, arr, idx, type

compile_opt idl2

print , n_elements(arr)

;;判斷輸入陣列是否為2*n

if ~n_elements(arr) then begin

message, 'incorrect number of arguments', /continue

return, !null

endif else begin

if (size(arr, /dimensions))[0] ne 2 $

or size(arr, /n_dimensions) ne 2 then begin

message, 'please input array with 2*n dimensions', /continue

return, !null

endif

endelse

;判斷按第幾列排序,預設為0

;0 --- 按第一列先排序

;1 --- 按第二列先排序

if ~n_elements(idx) then begin

idx = 0

endif else begin

if idx ne 0 and idx ne 1 then begin

message, 'input index must be one of the value:0,1', /continue

return, !null

endif

endelse

;判斷排序型別,降序or公升序

;0 - 均按公升序排序

;1 - 均按降序排序

;2 - 首先按公升序,然後按降序

;3 - 首先按降序,然後按公升序

if ~n_elements(type) then begin

type = 0

endif

arr1 = arr[1-idx,*]

arr2 = arr[idx,*]

case type of

0: begin

arr1sort = arr1[sort(arr2)]

arr2sort = arr2[sort(arr2)]

r = histogram(arr2sort, location = loc)

evalue = loc[where(histogram(arr2sort) gt 1)]

foreach element, evalue do begin

eidx = where(arr2sort eq element)

arr1sort[eidx] = (arr1sort[eidx])[sort(arr1sort[eidx])]

endforeach

end1: begin

arr1sort = reverse(arr1[sort(arr2)])

arr2sort = reverse(arr2[sort(arr2)])

r = histogram(arr2sort, location = loc)

evalue = loc[where(histogram(arr2sort) gt 1)]

foreach element, evalue do begin

eidx = where(arr2[reverse(sort(arr2))] eq element)

arr1sort[eidx] = (arr1sort[eidx])[(reverse(sort(arr1sort[eidx])))]

endforeach

end2: begin

arr1sort = (arr1[sort(arr2)])

arr2sort = (arr2[sort(arr2)])

r = histogram(arr2sort, location = loc)

evalue = loc[where(histogram(arr2sort) gt 1)]

foreach element, evalue do begin

eidx = reverse(where(arr2sort eq element))

arr1sort[eidx] = ((arr1sort[eidx])[((sort(arr1sort[eidx])))])

endforeach

end3: begin

arr1sort = reverse(arr1[sort(arr2)])

arr2sort = reverse(arr2[sort(arr2)])

r = histogram(arr2sort, location = loc)

evalue = loc[where(histogram(arr2sort) gt 1)]

foreach element, evalue do begin

eidx = (where(arr2sort eq element))

arr1sort[eidx] = ((arr1sort[eidx])[((sort(arr1sort[eidx])))])

endforeach

endelse: begin

message, 'please input the correct argument of type.', /continue

return, !null

endendcase

arrnew = arr

arrnew[1-idx,*] = arr1sort

arrnew[idx,*] = arr2sort

return, arrnew

end

IDL 過程與函式

envi idl程式檔案以 pro 或者 function 開頭,以 end 結尾,需要先由idl編譯器編譯成程式模組,然後執行。idl中過程和函式儲存的檔案字尾都是 pro 作為副檔名。ascii碼檔案 乙個.pro副檔名的檔案裡面可以有多個pro,或者function,但必須有乙個主pro和檔名...

il和idl區別 IDL中常用的函式意思

字串章節 strtrim 字串的裁剪 strupcase 將字串轉成大寫 strmid 字串的提取 strlen 字串求長度 陣列章節 intarr 建立乙個整型陣列 btyarr 建立byte 型別陣列 bytscl 陣列轉換成 byte 型別fix 將其他型別的陣列轉換成整型陣列 sort 返回...

idl 原函式分析

pro course 13 定義檔案路徑 myrootdir d 3 遍歷資料夾 filearr file search myrootdir,gz count num for fileindex 0,num num,1 do begin 開啟檔案 openr lun filearr fileinde...