在R中,如果我们有一个数据矩阵,比如一个100乘10矩阵X,一个带有可能值(0,1,2,3)的100个元素矢量t,我们可以很容易地找到一个简单的X矩阵y句法:
y = X[t == 1, ]
Run Code Online (Sandbox Code Playgroud)
但是,问题是,我怎么能用Rcpp的NumericMatrix做到这一点?
(或者,更一般地说,我怎么能在C++的任何容器中做到这一点?)
感谢Dirk的暗示,似乎是这样
NumericMatrix X(dataX);
IntegerVector T(dataT);
mat Xmat(X.begin(), X.nrow(), X.ncol(), false);
vec tIdx(T.begin(), T.size(), false);
mat y = X.rows(find(tIdx == 1));
Run Code Online (Sandbox Code Playgroud)
可以做我想做的事,但这似乎太冗长了.
use*_*795 10
我很乐意将其视为糖.不幸的是,我没有资格实施它.以下是我玩过的许多不同的解决方案.
首先,我不得不做出一些修改功一撩代码得到这个工作(colvec
而不是vec
为tIdx
和Xmat.rows(...
,而不是X.rows(...
:
mat Xmat(X.begin(), X.nrow(), X.ncol(), false);
colvec tIdx(T.begin(), T.size(), false);
mat y = Xmat.rows(find(tIdx == 1));
Run Code Online (Sandbox Code Playgroud)
其次,这里有三个基准逻辑语句所有子集矩阵的基准功能.函数采用arma或rcpp参数和返回值两个基于Gong-Yi Liao的解决方案,一个是基于循环的简单解决方案.
n(行)= 100,p(T == 1)= 0.3
expr min lq median uq max
1 submat_arma(X, T) 5.009 5.3955 5.8250 6.2250 28.320
2 submat_arma2(X, T) 4.859 5.2995 5.6895 6.1685 45.122
3 submat_rcpp(X, T) 5.831 6.3690 6.7465 7.3825 20.876
4 X[T == 1, ] 3.411 3.9380 4.1475 4.5345 27.981
Run Code Online (Sandbox Code Playgroud)
n(行)= 10000,p(T == 1)= 0.3
expr min lq median uq max
1 submat_arma(X, T) 107.070 113.4000 125.5455 141.3700 1468.539
2 submat_arma2(X, T) 76.179 80.4295 88.2890 100.7525 1153.810
3 submat_rcpp(X, T) 244.242 247.3120 276.6385 309.2710 1934.126
4 X[T == 1, ] 229.884 236.1445 263.5240 289.2370 1876.980
Run Code Online (Sandbox Code Playgroud)
submat.cpp
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
using namespace arma;
// arma in; arma out
// [[Rcpp::export]]
mat submat_arma(arma::mat X, arma::colvec T) {
mat y = X.rows(find(T == 1));
return y;
}
// rcpp in; arma out
// [[Rcpp::export]]
mat submat_arma2(NumericMatrix X, NumericVector T) {
mat Xmat(X.begin(), X.nrow(), X.ncol(), false);
colvec tIdx(T.begin(), T.size(), false);
mat y = Xmat.rows(find(tIdx == 1));
return y;
}
// rcpp in; rcpp out
// [[Rcpp::export]]
NumericMatrix submat_rcpp(NumericMatrix X, LogicalVector condition) {
int n=X.nrow(), k=X.ncol();
NumericMatrix out(sum(condition),k);
for (int i = 0, j = 0; i < n; i++) {
if(condition[i]) {
out(j,_) = X(i,_);
j = j+1;
}
}
return(out);
}
/*** R
library("microbenchmark")
# simulate data
n=100
p=0.3
T=rbinom(n,1,p)
X=as.matrix(cbind(rnorm(n),rnorm(n)))
# compare output
identical(X[T==1,],submat_arma(X,T))
identical(X[T==1,],submat_arma2(X,T))
identical(X[T==1,],submat_rcpp(X,T))
# benchmark
microbenchmark(X[T==1,],submat_arma(X,T),submat_arma2(X,T),submat_rcpp(X,T),times=500)
# increase n
n=10000
p=0.3
T=rbinom(n,1,p)
X=as.matrix(cbind(rnorm(n),rnorm(n)))
# benchmark
microbenchmark(X[T==1,],submat_arma(X,T),submat_arma2(X,T),submat_rcpp(X,T),times=500)
*/
Run Code Online (Sandbox Code Playgroud)
我所知道的最接近的是组合find()
函数组合submat()
函数犰狳通过访问RcppArmadillo.
编辑:这当然是我们可以通过补丁添加的东西.如果有人有足够的动力试试这个,请访问rcpp-devel邮件列表.