Dear all;

In c++ I want to do:
x<-c(2,4,6,8,10)
i<- c(2,3,5)
x[i]

I do:

cat("
#include <RcppArmadillo.h>

using namespace Rcpp;
using namespace arma;

// [[Rcpp::export]]
NumericVector select(NumericVector vec_, NumericVector idx_){
  arma::vec vv  = Rcpp::as<arma::vec>(vec_);
  arma::uvec ii = Rcpp::as<arma::uvec>(idx_);
  arma::vec aa  = vv.elem(ii-1);
  Rcpp::NumericVector bb(as<Rcpp::NumericMatrix>(wrap(aa)));
  return Rcpp::wrap( bb );
}
", file="select.cpp")

Rcpp::sourceCpp('./select.cpp')
select(x, i)

     [,1]
[1,]    4
[2,]    6
[3,]   10

Two things: 1) there must be easier ways and 2) why is the result a matrix and 
not a vector (i.e. why does the result have a dim attribute)?

Cheers
Søren

_______________________________________________
Rcpp-devel mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Reply via email to