与逻辑语句匹配的Rcpp Matrix子集 [英] Subset of a Rcpp Matrix that matches a logical statement
问题描述
y = X [t == 1,]
但是,问题是,我如何使用Rcpp的NumericMatrix?
(或者,
感谢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));
可以做我想要的,但是看起来太长了。
解决方案我最了解的是
find()
功能与 Armadillo 中的submat()
功能相结合 RcppArmadillo 。
编辑:这是我们可以通过补丁添加的。如果有人有足够的动机尝试这个,请到rcpp-devel邮件列表。
In R, if we have a data matrix, say a 100 by 10 matrix X, and a 100-elements vector t with possible values (0, 1, 2, 3), we can easily find a submatrix y of X using a simple syntax:
y = X[t == 1, ]
But, the problem is, how can I do that with Rcpp's NumericMatrix ?
(Or, more generally, how can I do that in C++'s any containers ?)Thanks to Dirk's hint, it seems that
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));
Can do what I want, but that seems too lengthy.
解决方案The closest I know of is the combination of the
find()
function combined with thesubmat()
function in Armadillo accessible via RcppArmadillo.Edit: This is course something we could add via a patch. If anybody is sufficiently motivated to try this, please come to the rcpp-devel mailing list.
这篇关于与逻辑语句匹配的Rcpp Matrix子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!