Find row- or columnwise the index of the maximal / minimal element in a matrix
Source:R/getMaxColIndex.R
getMaxIndexOfRows.RdgetMaxIndexOfRows returns the index of the maximal element of each row.
getMinIndexOfRows returns the index of the minimal element of each row.
getMaxIndexOfCols returns the index of the maximal element of each col.
getMinIndexOfCols returns the index of the minimal element of each col.
If a corresponding vector (row or col) is empty, possibly after NA removal, -1 is returned
as index.
Usage
getMaxIndexOfRows(x, weights = NULL, ties.method = "random", na.rm = FALSE)
getMinIndexOfRows(x, weights = NULL, ties.method = "random", na.rm = FALSE)
getMaxIndexOfCols(x, weights = NULL, ties.method = "random", na.rm = FALSE)
getMinIndexOfCols(x, weights = NULL, ties.method = "random", na.rm = FALSE)Arguments
- x
[
matrix(n,m)]
Numerical input matrix.- weights
[
numeric]
Weights (same length as number of rows/cols). If these are specified, the index is selected from the weighted elements (seegetMaxIndex). Default isNULLwhich means no weights.- ties.method
[
character(1)]
How should ties be handled? Possible are: “random”, “first”, “last”. Default is “random”.- na.rm
[
logical(1)]
IfFALSE, NA is returned if an NA is encountered inx. IfTRUE, NAs are disregarded. Default isFALSE
Examples
x = matrix(runif(5 * 3), ncol = 3)
print(x)
#> [,1] [,2] [,3]
#> [1,] 0.40353812 0.6783804 0.05144628
#> [2,] 0.06366146 0.7353196 0.53021246
#> [3,] 0.38870131 0.1959567 0.69582388
#> [4,] 0.97554784 0.9805397 0.68855600
#> [5,] 0.28989230 0.7415215 0.03123033
print(getMaxIndexOfRows(x))
#> [1] 2 2 3 2 2
print(getMinIndexOfRows(x))
#> [1] 3 1 2 3 3