SCOLR

Sorts columns of a real rectangular matrix using keys in rows.

Required Arguments

XNRX by NCX matrix. (Input, if IRET = 1; input/output if IRET = 0)
On input, X contains the matrix to be sorted. If IRET = 0, the output X contains the sorted matrix.

INDKEY — Vector of length NKEY giving the row numbers of X which are to be used in the sort. (Input)

IPERM — Permutation vector of length NCX specifying the rearrangement of the columns. (Output)
IPERM (I) = J means column I of the sorted X is column J of the unsorted X.

NGROUP — Number of groups. (Output)
The columns of the sorted X are partitioned into groups. A group contains columns that are equal with respect to the method of comparison. NGROUP is the number of groups of different columns.

NI — Vector of length NGROUP containing the number of columns in each group. (Output)
The first NI(1) columns of the sorted X are group number 1; the next NI(2) columns of the sorted X are group number 2; the last NI(NGROUP) columns of the sorted X are group number NGROUP. If NGROUP is not known prior to the invocation of this routine, NCX(an upper bound for NGROUP) can be used as the dimension of NI.

Optional Arguments

NRX — Number of rows of X. (Input)
Default: NRX = size (X,1).

NCX — Number of columns of X. (Input)
Default: NCX = size (X,2).

LDX — Leading dimension of X exactly as specified in the dimension statement in the calling program. (Input)
Default: LDX = size (X,1).

ICOMP — Option giving the method of comparison of the column vectors. (Input)
Default: ICOMP = 0.

 

ICOMP

Action

0

Elementwise, by algebraic values

1

Elementwise, by absolute values

IORDR — Option giving the sorting order. (Input)
Default: IORDR = 0.

 

IORDR

Action

0

Ascending

1

Descending

IRET — Option for determining whether the columns of X are to be permuted. (Input)
Default: IRET = 0.

 

IRET

Action

0

The columns of X are sorted.

1

X is unchanged (detached key sort).

NKEY — Number of rows of X on which to sort. (Input)
Default: NKEY = size (INDKEY,1).

FORTRAN 90 Interface

Generic: CALL SCOLR (X, INDKEY, IPERM, NGROUP, NI [])

Specific: The specific interface names are S_SCOLR and D_SCOLR.

FORTRAN 77 Interface

Single: CALL SCOLR (NRX, NCX, X, LDX, ICOMP, IORDR, IRET, NKEY, INDKEY, IPERM, NGROUP, NI)

Double: The double precision name is DSCOLR.

Description

Routine SCOLR sorts the columns of a real matrix X using particular rows in X as the keys. One of two methods for comparing the columns can be used for sorting.

1. Algebraic with the first key as the most significant, the second key next most significant and so forth.

2. Absolute values with the first key as the most significant, the second key next most significant and so forth.

The columns of X can be put in ascending or descending order.

The routine is useful for data containing classification variables. Routine CSTAT (see Chapter 1, “Basic Statistics”) can be used to form the cells and frequency counts for a multi‑way table from data. The columns of the output matrix contain the values of each combination of values of the classification variables along with the tallies. SCOLR can then be used to sort the columns of this output matrix using the classification variables as keys.

SCOLR is based on a quicksort method given by Singleton (1969). Modifications by Griffin and Redish (1970) and Petro (1970) are incorporated.

Comments

1. Workspace may be explicitly provided, if desired, by use of S2OLR/DS2OLR. The reference is:

CALL S2OLR (NRX, NCX, X, LDX, ICOMP, IORDR, IRET, NKEY, INDKEY, IPERM, NGROUP, NI, WK, IWK)

The additional arguments are as follows:

WK — Work vector of length 2 * m.

IWK — Work vector of length m + INT(2.8854 ln(m)) + 2.

2. When X is sorted by algebraic value (ICOMP = 0) in ascending order, the resulting array X is such that:
For i = 1, 2, , NCX – 1, X(INDKEY(1), i) X(INDKEY(1), i + 1)
For k = 2, , NKEY, if X(INDKEY(j), i) = X(INDKEY(j), i + 1) for j = 1, 2, , k  1, then X(INDKEY (k), i X(INDKEY(k), i + 1).
When ICOMP = 1, the absolute values are compared instead.

Example

The columns of a 5 x 10 matrix X are sorted in descending order by absolute value using rows 1, 2, 3, and 5 as the keys. The permutations to put the columns of X in order are returned. The input matrix X is not changed.

 

USE SCOLR_INT

USE WRRRL_INT

USE WRIRL_INT

USE UMACH_INT

 

IMPLICIT NONE

INTEGER LDX, NCX, NKEY, NRX

PARAMETER (NCX=10, NKEY=4, NRX=5, LDX=NRX)

!

INTEGER ICOMP, INDKEY(NKEY), IORDR, IPERM(NCX), IRET, NI(NCX), &

NGROUP, NOUT

REAL X(LDX,NCX)

CHARACTER CLABEL(1)*10, FMT*10, RLABEL(1)*23

!

DATA CLABEL(1)/'NONE'/, RLABEL(1)/'NONE'/

DATA X/-1.0, -10.0, -11.0, 10.0, -1.0, 2.0, 20.0, 22.0, -20.0, &

-2.0, -3.0, -30.0, 33.0, 30.0, -3.0, 4.0, 40.0, 44.0, &

-40.0, -4.0, -5.0, -50.0, 55.0, 50.0, -5.0, -1.0, 60.0, &

-66.0, -60.0, 6.0, 2.0, -70.0, -77.0, 70.0, 7.0, -3.0, &

-30.0, -88.0, 80.0, 8.0, 4.0, 40.0, -99.0, -90.0, 9.0, &

-5.0, -50.0, -100.0, 100.0, 10.0/

DATA INDKEY/1, 2, 3, 5/

!

ICOMP = 1

IORDR = 1

IRET = 1

CALL SCOLR (X, INDKEY, IPERM, NGROUP, NI, ICOMP=ICOMP, &

IORDR=IORDR, IRET=IRET)

!

FMT = '(F6.1)'

RLABEL(1) = 'NONE'

CALL WRRRL ('X', X, RLABEL, CLABEL)

!

FMT = '(I4)'

RLABEL(1) = 'IPERM = '

CALL WRIRL ('%/', IPERM, RLABEL, CLABEL, 1, NCX, 1, FMT='(I4)')

!

CALL UMACH (2, NOUT)

WRITE (NOUT,*)

WRITE (NOUT,*) 'NGROUP = ', NGROUP

!

RLABEL(1) = 'NI = '

CALL WRIRL ('%/', NI, RLABEL, CLABEL, 1, NGROUP, 1, FMT='(I4)')

!

END

Output

 

X

-1.0 2.0 -3.0 4.0 -5.0 -1.0 2.0 -3.0 4.0 -5.0

-10.0 20.0 -30.0 40.0 -50.0 60.0 -70.0 -30.0 40.0 -50.0

-11.0 22.0 33.0 44.0 55.0 -66.0 -77.0 -88.0 -99.0 -100.0

10.0 -20.0 30.0 -40.0 50.0 -60.0 70.0 80.0 -90.0 100.0

-1.0 -2.0 -3.0 -4.0 -5.0 6.0 7.0 8.0 9.0 10.0

IPERM = 10 5 9 4 8 3 7 2 6 1

NGROUP = 10

NI = 1 1 1 1 1 1 1 1 1 1