File: matsort.R

package info (click to toggle)
r-cran-geometry 0.3-6%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,184 kB
  • sloc: ansic: 366; xml: 202; sh: 13; makefile: 5
file content (23 lines) | stat: -rw-r--r-- 608 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"matsort" <-
function (...) {
    x = cbind(...)
    if(!is.numeric(x))
        stop("Input should by numeric.")
    res = array(dim = c(nrow(x), 0))
    if (!is.matrix(drop(x)))
        return(x)
    else if (ncol(x) > 30)
        return(t(apply(x, 1, sort)))
    else while (is.matrix(drop(x))) {
        imc = max.col(x)
        x = t(x)
        imx = nrow(x) * (1:ncol(x) - 1) + imc
        xmax = x[imx]
        x = t(matrix(x[-imx], ncol = ncol(x)))
        res = cbind(res, xmax)
    }
    res = cbind(res, x)
    colnames(res) = NULL
    rownames(res) = NULL
    return(res)
}