I am using RCPP to speed up the R code in my project. Now what I am doing is to transfer my R code into C++ using Armadillo package. I found I often write multiple lines in C++ to replace one line in R...
Here is my question: I have a vector stored data: Data. Also I have a matrix stored the index of elements I need to access. Please allow me to illustrate my scenario in R first:
> Data
[1] 4 5 6 7 8
And
> index
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 1 0 0
[3,] 2 0 2
For each row of "index" matrix, I want to get the corresponding elements from the data. In R, I only need to code like this:
> Data[index[1,]]
[1] 4 5 6
> Data[index[2,]]
[1] 4
> Data[index[3,]]
[1] 5 5
i.e. the elements I need from the first row of 'index' matrix is Data[1],Data[2],Data[3]
the elements I need from the 2nd row of 'index' matrix is Data[1]
the elements I need from the 3rd row of 'index' matrix is Data[2] Data[2]
The convenience of R is that R automatically identifies the 0 index as 'nothing' and won't access it.
Now I input the vector 'Data' and matrix 'index' into C now. I was wondering is there any way to achieve the similar result as R above? Thanks a lot!