[Rcpp-devel] How to extract rows or columns by vector of indices
Andy Leung
andy.leung at stat.ubc.ca
Sat Feb 11 00:56:02 CET 2012
Dear Rcpp users and developers,
I understand how to extract a single column or row from an
RcppArmadillo matrix, but I am still unsure how to extract columns or
rows based on a vector of indices. I wrote a rather awkward (and dumb)
way to extract, and here are some reproducible codes:
###############################
library(Rcpp)
library(RcppArmadillo)
library(inline)
src <-
' arma::mat x = arma::randu(5, 5);
arma::mat y = x;
arma::uvec indice;
indice << 0 << 3;
int nRm = indice.n_elem;
for(int j = 0; j < nRm; j++)
y.shed_col( indice(j) - j );
return List::create(Named("x")=x, Named("y")=y, Named("ind")=indice);'
colX <- cxxfunction(body=src,plugin="RcppArmadillo")
colX()
###############################
Although this code works, I am wondering if there are other more
clever ways of approaching this, especially a way to avoid any 'for'
loops.
Thank you for your help!
Andy Leung
More information about the Rcpp-devel
mailing list