2

我有一个大矩阵,比如这个:

NCols=100
NRows=100

myMat<-matrix(runif(NCols*NRows), ncol=NCols)

我有兴趣找到最接近矩阵所有值的平均值或中值的行和列,计算为mean(myMat).

我怎么能做到这一点R

4

1 回答 1

7

尝试这个:

set.seed(45) # just for reproducibility
NCols <- 100
NRows <- 100
myMat <- matrix(runif(NCols*NRows), ncol=NCols)

mat_minus_mean <- abs(myMat - mean(myMat))
idx <- which(mat_minus_mean == min(mat_minus_mean), arr.ind = TRUE)
#      row col
# [1,]   5  33

> myMat[idx]
# [1] 0.5012305 # mean(myMat) is 0.5012474
于 2013-03-11T17:40:39.860 回答