NCL Home > Documentation > Functions > General applied math

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

x

An integer, float, or double array of any dimensionality

kflag

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

qsort, sqsort

Examples

Example 1

Let x(ntim) generate a permutation vector in

Example 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 3

Define x(ntim, nlat, mlon) with named dimensions "time","lat", "lon". Then:

  ip = dim_pqsort(x(lat|:, lon|:, time|:), 1) 
; ===> ip(nlat, mlon, ntim)