If M is a numeric matrix, I can order its rows with respect to the lexicographical order by running lexsort(M) where
lexorder <- function(M) {
do.call(order, lapply(seq_len(ncol(M)), function(i) M[, i]))
}
lexsort <- function(M) {
M[lexorder(M), ]
}
But I'm only interested in getting the greater row (the last one of the ordered matrix). Can we avoid ordering everything to more efficiently extract the last row?
You couls write a recursive function that works faster:
The timings:
Large number of columns:
Large number of rows
In all the instances the
lex_maxfunction performs >~10x fasterEdit:
If you need the position, you could simply do: