mercredi 6 mai 2015

Using colon (':') to access elements in an array in C++ (in Rcpp)

I am trying to run the following code. Frankly I know C++ only little but I want to get the following function run. Can you help me run this silly example?

cppFunction(
  'NumericVector abc(int x, int x_end, NumericVector y)
  {
    NumericVector z;
    int x1 = x + x_end;
    z = y[x:x1];
    return(z);  
   }'
)

abc(3,c(0,1,10,100,1000,10000))

I see this ...

error: expected ']' before ':' token

Update Sorry I forgot to mention that I need to generate a sequence of numbers from x to x1. The function IntegerVector::create only creates a variable with only x and x1 not x though x1. The example I gave was trivial. I updated the example now. I need to do in C++ what seq() does in R

Aucun commentaire:

Enregistrer un commentaire