dim_pqsort
Computes the permutation vector generated by sorting the n - 1th (rightmost) dimension.
Prototype
function dim_pqsort ( x : integer, float or double, kflag : integer ) return_val [dimsizes(x)] : integer
Arguments
xAn integer, float, or double array of any dimensionality
kflag- 2 - return the permutation vector resulting from sorting x in increasing order and sort x also.
- 1 - return the permutation vector resulting from sorting x in increasing order and do not sort x.
- -1 - return the permutation vector resulting from sorting x in decreasing order and do not sort x.
- -2 - return the permutation vector resulting from sorting x in decreasing order and sort x also.
Return value
Returns an integer array dimensioned the same size as x.
Description
This function returns the permutation array generated by sorting the rightmost (n - 1th) dimension of the x array and, optionally, rearranging the elements of the array. The array will be sorted in increasing or decreasing order. A slightly modified quicksort algorithm is used.
Ignoring missing values is not supported; they are sorted to either end of the array based on their actual value.
The output permutation array will be the index of the value in the original order of the x array that is in the i-th location in the sorted order.
See Also
Examples
Example 1
Let x(ntim) generate a permutation vector in
- ascending order, do not sort x:
ip = dim_pqsort(x, 1)
- descending order, do not sort x:
ip = dim_pqsort(x, -1)
- ascending order, sort x also:
ip = dim_pqsort(x, 2)
- descending order, sort x also:
ip = dim_pqsort(x, -2)
Define ip = dim_pqsort(x, 1) (ascending order, do not sort x) and it is desired to reorder a different array (say, y) using the permutation vector associated with x. Assume y(ntim, nlat, mlon) with named dimensions and coordinate variables "time", "lat" and "lon". Then:
y2 = y ; create yNew with attribute, coordinate values
y2 = y(ip, :, :) ; yNew will contain the values of "y"
; rearranged according to the permutation
; vector associated with x.
y2&time = y&time(ip) ; reorder the time coordinate variable
Example 3Define x(ntim, nlat, mlon) with named dimensions "time","lat", "lon". Then:
ip = dim_pqsort(x(lat|:, lon|:, time|:), 1) ; ===> ip(nlat, mlon, ntim)