R_Language 板


LINE

※ 引述《chu1216 (chu)》之铭言: : 请问一下 : 我想要找非零的index的矩阵, : 因此我用which(XXX != 0, arr.ind = T), : 但因为矩阵的size非常大, 跑起来花很长时间, : 请问有类似且效率比较好的的function吗? : 感谢!! 我试了一下RcppArmadillo 做了一个小试验 从结果来看,可能C++可以帮上一点忙,但是要看你sparse的比例 程式码: library(Rcpp) sourceCpp(code = " // [[Rcpp::depends(RcppArmadillo)]] #include <RcppArmadillo.h> // [[Rcpp::export]] arma::uvec find_nonzero_index(const arma::mat & x) { return find(x != 0) + 1; }") genSparseMat <- function(matSize, sparseProp) { x <- matrix(runif(prod(matSize)), matSize[1]) x[x >= sparseProp] <- 0 return(x) } set.seed(100) matSp30 <- genSparseMat(c(5000, 5000), 0.3) matSp20 <- genSparseMat(c(5000, 5000), 0.2) matSp10 <- genSparseMat(c(5000, 5000), 0.1) matSp05 <- genSparseMat(c(5000, 5000), 0.05) matSp01 <- genSparseMat(c(5000, 5000), 0.01) library(microbenchmark) microbenchmark( RWhichSp30 = which(matSp30 != 0), RcppSp30 = find_nonzero_index(matSp30), RWhichSp20 = which(matSp20 != 0), RcppSp20 = find_nonzero_index(matSp20), RWhichSp10 = which(matSp10 != 0), RcppSp10 = find_nonzero_index(matSp10), RWhichSp05 = which(matSp05 != 0), RcppSp05 = find_nonzero_index(matSp05), RWhichSp01 = which(matSp01 != 0), RcppSp01 = find_nonzero_index(matSp01), times = 20L ) 结果: Unit: milliseconds expr min lq mean median uq max neval RWhichSp30 167.1455 182.53765 195.99211 189.95580 207.34010 254.4168 20 RcppSp30 120.1717 125.33950 134.36280 128.67385 134.05210 216.7681 20 RWhichSp20 141.7320 148.71530 164.81187 159.17785 173.29780 224.4695 20 RcppSp20 92.8394 94.95830 99.97492 96.96545 102.32200 122.8803 20 RWhichSp10 118.1888 127.71755 138.10948 136.37605 150.66480 162.9312 20 RcppSp10 53.6464 55.00425 59.48229 59.18855 63.43540 68.5329 20 RWhichSp05 106.1757 111.50920 127.11906 117.38275 133.84795 231.4787 20 RcppSp05 35.1256 36.11950 38.13195 38.11350 39.82205 41.8294 20 RWhichSp01 95.0594 102.33750 113.32983 107.19800 124.01145 150.2782 20 RcppSp01 19.9087 20.64855 21.85202 21.43630 22.75725 26.0881 20 可能的方向: 1. 计算中,产生matrix的时候,是否就可以直接是SparseMatrix,而不用转换 ex: 用Matrix::sparse.model.matrix 而非使用model.matrix 2. 整个计算移到RcppArmadillo的架构,透过C++加速回圈 (R回圈很慢) 3. 重新设计演算法,避免取大量非零元素的index 4. 写一个OpenMP的C++函数帮你用多个cores跑 因为你没有提供其他细节,所以我只能提供这样的方向建议 如果你有一个最小可重现的程式的话,欢迎PO上来 我或是部分有闲的板友应该可以帮忙看看 --
QR Code



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 114.32.179.120 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/R_Language/M.1663154628.A.7B0.html ※ 编辑: celestialgod (114.32.179.120 台湾), 09/14/2022 19:42:20
1F:推 lycantrope: arma::find预设回传nonzero index,稀疏程度好像只差 09/15 13:01
2F:→ lycantrope: 在输出矩阵大小。nonzero越多所需时间越多 09/15 13:02







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:BabyMother站内搜寻

TOP