Learn R Programming

dvmisc (version 1.1.4)

which_max_nm: Return (Row, Column) Index of (First) Maximum of a Numeric Matrix

Description

Written in C++, this function tends to run much faster than the equivalent (if maximum is unique) base R solution which(x == max(x), arr.ind = TRUE).

Usage

which_max_nm(x)

Arguments

x

Numeric matrix.

Value

Integer vector.

Details

For optimal speed, choose the version of this function that matches the class of your x:

which_max_nv for numeric vector. which_max_iv for integer vector. which_max_nm for numeric matrix. which_max_im for integer matrix.

Examples

Run this code
# NOT RUN {
# which_max_nm is typically much faster than 
# which(x == max(x), arr.ind = TRUE)
x <- matrix(rnorm(100), ncol = 10)
all(which(x == max(x), arr.ind = TRUE) == which_max_nm(x))
benchmark(which(x == max(x), arr.ind = TRUE), which_max_nm(x),
          replications = 5000)

# }

Run the code above in your browser using DataLab