0
是否可以遍历矩阵并对某个子集进行一些分析?Rcpp循环和子集数字矩阵
在R:
for(i in 10:nrow(mat)){
hist = mat[(i-5):(i),]
// Do something
}
在上述R例子中,我通过从第10行的矩阵mat
它的最后一行循环。在每次迭代中,我将最近的5次迷惑集合起来并做一些事情。
这是可能的Rcpp?下面的例子是我试过..
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;
}
在行//NumericMatrix hist = res;
,我尝试将其转换回输入NumericMatrix但它失败。
请转到[Rcpp Gallery](http://gallery.rcpp.org)并查看子集示例来回答此问题。简而言之:_sure!_ –