I have a matrix defined in R. I need to pass this matrix to a c++ function and do operations in C++.
Example: In R, define a matrix,
A <- matrix(c(9,3,1,6),2,2,byrow=T)
PROTECT( A = AS_NUMERIC(A) );
double* p_A = NUMERIC_POINTER(A);
I need to pass this matrix to a C++ function where variable ‘data’ of type vector<vector<double>> will be initialized with the matrix A.
I couldn’t seem to figure out how to do this. I am thinking in more complicated way then I should be, I bet there is an easy way to do this.
As Paul said, I would recommend using
Rcppfor that kind of things. But it also depends what you want yourvector< vector<double> >to mean. Assuming you want to store columns, you might process your matrix like this: