[Rcpp-devel] How to extract rows or columns by vector of indices

Dirk Eddelbuettel edd at debian.org
Sat Feb 11 07:29:50 CET 2012


On 10 February 2012 at 15:56, Andy Leung wrote:
| 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

I use span() methods a lot too, but thety are continuous...

| 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.

Not that I know off.  It's an area ripe for further contributions....

Dirk


| 
| Thank you for your help!
| Andy Leung
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

-- 
"Outside of a dog, a book is a man's best friend. Inside of a dog, it is too
dark to read." -- Groucho Marx


More information about the Rcpp-devel mailing list