0
votes

Good afternoon ,

I created this rcpp function that returns a 3d matrix with dimensions K * K * L.

Rcpp Code :

#include <Rcpp.h>
using namespace Rcpp;   

// [[Rcpp::export]]

NumericVector Test( int K , int L ){

  // calling some R functions 

  Function f1("rnorm"); 
  Function f2("runif"); 
  Function f3("ceiling"); 
  Function f4("unique"); 
  Function f5("array"); 
  Function f6("unique.array"); 
  Function f7("print");

  // Creating a vector of normal law with length= K * K * L

  NumericVector rn = f5(f1(K*K*L, Named("sd")=1, _["mean"]=0), Named("dim")=NumericVector::create(K,K,L));

  // Reshaping the 1d vector  , the 3d matrix will be visible in R 
  rn.attr("dim") = Dimension(K, K , L ); 
      return rn ;
    }

***Example of the obtained results :***  The function is called in Rstudio

    > Test(3,2)
    , , 1

            [,1]       [,2]        [,3]
[1,] -0.57144807 -0.1650806  0.07607133
[2,]  0.61278268 -0.5049861  0.37705107
[3,]  0.09037529  1.6185765 -0.68675536

, , 2

           [,1]       [,2]       [,3]
[1,] -0.5789173 -0.6630934  0.9961613
[2,] -0.4425025 -0.9083358 -1.4860523
[3,]  0.2150056 -0.3229108 -1.6536952

My question :

  • Suppose that (K,L)=(3,2) like the example , how can i extract in rcpp the second row / column in the second table of rn (indexed by [ , , 2 ] ) ?

  • Is it possible to store this extracted vector of length=K in a numericVector variable using the vector.import() method ?

Sorry but i didn't find an example that could help me !

Thank you a lot !

1
Welcome to StackOverflow! If you could, please edit your question to i) remove the extra blank lines as more concise display is better and ii) remove the image and replace it with copy and pasted text. Thank you. - Dirk Eddelbuettel
@ Dirk Eddelbuettel , i think is difficult to me to remove the picture. I will try to remove the extra blank lines. - Tou Mou
You can mark several lines in any text based terminal, including RStudio, select copy and paste here. Please conform to the minimal convention to make it easier for us to help you. I appreciate your understanding in the matter. - Dirk Eddelbuettel
Thank you. I would also recommend to remove your first and last comment (both are just noise) and to indent the remaining overly long code line so the the complete code is displayed at once. Function f2,f3,f4,f6,f7 are unused, so why not remove them from the code snippet? Remember, minimally complete verifiable examples. Lastly, you seem to be "writing R code in C++". Based on my 10+ years experience with Rcpp, not the best way to go about it. - Dirk Eddelbuettel
@Dirk Eddelbuettel , the functions f2 , f3 ... f7 are used. C++ could give me the performance i want. I'm working in a classification project and i prefer to write a part with nested loops in c++. - Tou Mou

1 Answers

0
votes

A possible solution could be :

NumericVector v1 ; 
NumericVector v2=NumericVector::create(1,2,4,5,6,7,8) ; 
v1= rd.import( v2.begin() , v2.begin()+ 6) ; 
// return elements 1,2,4,5,6,7