与逻辑语句匹配的Rcpp Matrix子集 [英] Subset of a Rcpp Matrix that matches a logical statement

查看:197
本文介绍了与逻辑语句匹配的Rcpp Matrix子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在R中,如果我们具有数据矩阵,例如100×10的矩阵X和具有可能值(0,1,2,3)的100元素矢量t,则可以容易地找到X的子矩阵y使用一个简单的语法:

  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 the submat() 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆