如何用R中的向量元素来划分matrix的每一行

我想用一个固定的向量来划分matrix的每一 。 例如

mat<-matrix(1,ncol=2,nrow=2,TRUE) dev<-c(5,10) 

mat/devmat/dev每个

  [,1] [,2] [1,] 0.2 0.2 [2,] 0.1 0.1 

不过,我想这样做,即操作:

 rbind(mat[1,]/dev, mat[2,]/dev) [,1] [,2] [1,] 0.2 0.1 [2,] 0.2 0.1 

有明确的命令去那里?

以下是增加代码长度顺序的几种方法:

 t(t(mat) / dev) mat / dev[col(mat)] # @DavidArenburg & @akrun mat %*% diag(1 / dev) sweep(mat, 2, dev, "/") t(apply(mat, 1, "/", dev)) mat / rep(dev, each = nrow(mat)) mat / t(replace(t(mat), TRUE, dev)) library(plyr); aaply(mat, 1, "/", dev) do.call(rbind, lapply(as.data.frame(t(mat)), "/", dev)) mat2 <- mat; for(i in seq_len(nrow(mat2))) mat2[i, ] <- mat2[i, ] / dev 

基准

代码的简洁和清晰可能比速度更重要,但为了完整起见,这里是一些使用10次重复然后100次重复的基准。

 library(microbenchmark) library(plyr) set.seed(84789) mat<-matrix(runif(1e6),nrow=1e5) dev<-runif(10) microbenchmark(times=10L, "1" = t(t(mat) / dev), "2" = mat %*% diag(1/dev), "3" = sweep(mat, 2, dev, "/"), "4" = t(apply(mat, 1, "/", dev)), "5" = mat / rep(dev, each = nrow(mat)), "6" = mat / t(replace(t(mat), TRUE, dev)), "7" = aaply(mat, 1, "/", dev), "8" = do.call(rbind, lapply(as.data.frame(t(mat)), "/", dev)), "9" = {mat2 <- mat; for(i in seq_len(nrow(mat2))) mat2[i, ] <- mat2[i, ] / dev}, "10" = mat/dev[col(mat)]) 

赠送:

 Unit: milliseconds expr min lq mean median uq max neval 1 7.957253 8.136799 44.13317 8.370418 8.597972 366.24246 10 2 4.678240 4.693771 10.11320 4.708153 4.720309 58.79537 10 3 15.594488 15.691104 16.38740 15.843637 16.559956 19.98246 10 4 96.616547 104.743737 124.94650 117.272493 134.852009 177.96882 10 5 17.631848 17.654821 18.98646 18.295586 20.120382 21.30338 10 6 19.097557 19.365944 27.78814 20.126037 43.322090 48.76881 10 7 8279.428898 8496.131747 8631.02530 8644.798642 8741.748155 9194.66980 10 8 509.528218 524.251103 570.81573 545.627522 568.929481 821.17562 10 9 161.240680 177.282664 188.30452 186.235811 193.250346 242.45495 10 10 7.713448 7.815545 11.86550 7.965811 8.807754 45.87518 10 

对所有那些花费20毫秒,重复100次的testing重新进行testing:

 microbenchmark(times=100L, "1" = t(t(mat) / dev), "2" = mat %*% diag(1/dev), "3" = sweep(mat, 2, dev, "/"), "5" = mat / rep(dev, each = nrow(mat)), "6" = mat / t(replace(t(mat), TRUE, dev)), "10" = mat/dev[col(mat)]) 

赠送:

 Unit: milliseconds expr min lq mean median uq max neval 1 8.010749 8.188459 13.972445 8.560578 10.197650 299.80328 100 2 4.672902 4.734321 5.802965 4.769501 4.985402 20.89999 100 3 15.224121 15.428518 18.707554 15.836116 17.064866 42.54882 100 5 17.625347 17.678850 21.464804 17.847698 18.209404 303.27342 100 6 19.158946 19.361413 22.907115 19.772479 21.142961 38.77585 100 10 7.754911 7.939305 9.971388 8.010871 8.324860 25.65829 100 

所以在这两个testing#2(使用diag )是最快的。 原因可能在于其对BLAS几乎直接的吸引力,而#1则依赖于更昂贵的t

您正在查找应用于行的apply函数:

 t(apply(mat, 1, function(x) x/dev))