Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rcpp sugar for NumericMatrix

Tags:

r

rcpp

For NumericVector, I can subset a smaller NumericVector by using an IntegerVector that contains the positions to subset.

e.g. suppose x<-c(1,2,2,3,4,5), idx<-c(1,3,4), and xsub<-x[idx] which is 1 2 3.

Within RCpp, I can simply use xsub=x[idx].

Is there a similar way to subset the rows of a NumericMatrix using the IntegerVector?

For example, the following code xmatsub=xmat(idx,_) didn't work for me.

like image 846
uday Avatar asked Jul 29 '26 11:07

uday


1 Answers

There isn't a way. You have to do it manually, which is not that complicated.

NumericMatrix res( idx.size(), m.rows() )  ;
for( int i=0; i<idx.size(); i++){
    res.row(i) = m.row(idx[i]-1) ; 
}
like image 193
Romain Francois Avatar answered Aug 01 '26 02:08

Romain Francois