我希望找到矩阵中的最大元素值以及其位置(在矩阵中的行和列ID)。
我正在使用以下函数返回矩阵的行和列。
这似乎是一种不好的方法——我可能错过了一种原生方法。有更好/更符合R风格的方法吗?
这是我的函数:
matxMax <- function(mtx)
{
colmn <- which(mtx == max(mtx)) %/% nrow(mtx) + 1
row <- which(mtx == max(mtx)) %% nrow(mtx)
return( matrix(c(row, colmn), 1))
}
我使用的方式如下:
mm <- matrix(rnorm(100), 10, 10)
maxCords <- matxMax(mm)
mm[maxCords]