RcppArmadillo:将矩阵子视图转换为SEXP

时间:2013-08-13 17:01:08

标签: r rcpp

有没有办法将arma::subview<double>转换为SEXP?在我的X代码中创建矩阵Rcpp之后,我有以下代码:

  //.... MCMC loop
  if(i % skiplength == 0) {
    for(unsigned int k=0; k < X.n_rows; k++) {
      X(k,index) = y[k];
    }
    index = index + 1;
  }
  // return matrix of samples in columns
  return Rcpp::wrap(X.submat( arma::span(0, X.n_rows - 1) , arma::span(discard, X.n_cols-1)));
END_RCPP
}

但是我收到一条错误消息,指出:cannot convert 'const arma::subview<double>' to SEXP in initialization。有一个简单的解决方案吗?

可重复的例子

reproducible = '
arma::subview<double> conv(arma::Mat<double> m) {
return m.submat(arma::span(0, m.n_rows),arma::span(0, m.n_cols));
}'

cppFunction(code = reproducible, depends = "RcppArmadillo", verbose = TRUE)

1 个答案:

答案 0 :(得分:3)

刚想出来,子视图显然可以转换为矩阵

我替换了

return Rcpp::wrap(X.submat( arma::span(0, X.n_rows - 1) , arma::span(discard, X.n_cols-1)));

arma::mat ret = X.submat(arma::span(0, X.n_rows - 1) , arma::span(discard, X.n_cols-1));
return Rcpp::wrap(ret);

它编译得很好。