Following on this question, I am trying to understand how to efficiently update a subset of a Rccp::NumericMatrix data type.
I have the following scenario:
Rcpp::NumericMatrix mof5 x 5that needs few rows and columns updated.- It will be passed by reference to a function (
voidreturn type) that will convert it to anarma::mat, and update the respectivesubmat(). - At this point I don't understand how to "apply" the changes that occurred inside the function to the
mmatrix that was passed to the function.
The code looks like this:
#include <iostream>
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
void updateMatrix(const Rcpp::NumericMatrix &m)
{
std::cout << m << std::endl;
Rcpp::as<arma::mat>(m).submat(0, 0, 3, 3) = Rcpp::as<arma::mat>(m).submat(0, 0, 3, 3) + 1;
std::cout << m << std::endl;
}
To run it from R I use:
m = matrix(0, 5, 5)
updateMatrix(m)
And the results are:
> updateMatrix(m)
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000
0.00000 0.00000 0.00000 0.00000 0.00000
It's the first time I'm using Rcpp and RcppArmadillo and they are absolutely amazing. I appreciate any help with this scenario.