有没有办法将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)
答案 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);
它编译得很好。