Is it possible to loop through a matrix and do some analysis of some subset?
In R:
for(i in 10:nrow(mat)){
hist = mat[(i-5):(i),]
// Do something
}
In the above R example, I am looping through the mat matrix from 10th row to its last row. On each iteration I subset the most recent 5 obseravations and do something.
Is this possible in Rcpp? The following example is something I tried..
int n_col = sample_data.ncol();
int n_row= sample_data.nrow();
int max_lb = 10;
for( int i=(max_lb+1); i<n_row; i++) {
SubMatrix<REALSXP> res = sample_data(Range(i-max_lb,i),Range(0,n_col));
//NumericMatrix hist = res; //If this is uncommented, it fails when I run it...it pretty much just freezes after some iteration...
Rcpp::Rcout << "-----------------------------" <<std::endl;
Rcpp::Rcout << i << "\n" <<std::endl;
Rcpp::Rcout << res .nrow() <<std::endl; // Dimensions do not match what I have
Rcpp::Rcout << res .ncol() <<std::endl;
}
In the line //NumericMatrix hist = res;, i try to convert it back to type NumericMatrix but it fails.