IDL uniq函數返回右相鄰不同值的該值索引

如果數組中從左往右遍歷,某索引處的該值不等於右相鄰值的話,則返回該值對應的在原始數組中的索引,例如

array = [1, 2, 1, 2, 3, 4, 5, 6, 6, 5]
IDL> uniq(array)
0 1 2 3 4 5 6 8 9

IDL> b = sort(array)
IDL> print,b
2 0 3 1 4 5 9 6 8 7

IDL> uniq(array,b)
0 1 4 5 6 7
指定參數b的話,就是對升序後的數組進行查找右相鄰不同數值的索引,如果右側相鄰數值不同於該值,則將該值索引返回,此處該值索引是sort()的返回值,即b數組的對應值。

加sort返回值的話,則是返回從小到大排列後的數組的該值不同於右相鄰值時該值在sort中的值做爲自己的索引。

IDL官方解釋

UNIQ
The UNIQ function returns the subscripts of the unique elements in an array. Note that repeated elements must be adjacent in order to be found. This routine is intended to be used with the SORT function: see the examples below. This function was inspired by the UNIX uniq(1) command.

This routine is written in the IDL language. Its source code can be found in the file uniq.pro in the lib subdirectory of the IDL distribution.

Examples
Find the unique elements of an unsorted array:

; Create an array:
array = [1, 2, 1, 2, 3, 4, 5, 6, 6, 5]

; Variable B holds an array containing the sorted,
; unique values in array:
b = array[UNIQ(array, SORT(array))]
PRINT, b

IDL prints

1 2 3 4 5 6

Syntax
Result = UNIQ( Array [, Index] )

Return Value
UNIQ returns an array of indices into the original array. Note that the index of the last element in each set of non-unique elements is returned. If the optional Index argument is supplied, then the last index in the order provided by the Index array is returned.

The following expression returns a copy of the sorted array with duplicate adjacent elements removed:

Array[UNIQ(Array)]

UNIQ returns 0 (zero) if the argument supplied is a scalar rather than an array.

Arguments
Array
The array to be scanned. For UNIQ to work properly, the array must be sorted into monotonic order unless the optional parameter Index is supplied.

Index
This optional parameter is an array of indices into Array that order the elements into monotonic order. That is, the expression:

Array[Index]

yields an array in which the elements of Array are rearranged into monotonic order.

The Index array can be generated from Array using the SORT function:

Index = SORT(Array)

If Array is not already in monotonic order, use the command:

UNIQ(Array, SORT(Array))


與其熱鬧的引人奪目,步步緊逼,不如逐向做一個人羣之中真實自信的人,不張揚,不虛飾,隨時保持後退的位,心有所定,只是專注做事。——素年錦時

版權歸作者 小白是哪個小白_ 所有,轉載、引用請註明鏈接出處。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章