【发布时间】:2020-12-10 02:08:15
【问题描述】:
我正在学习 Rcpp 软件包的功能,之前没有使用 C++ 的经验。我试过了:
#include <RcppArmadillo.h>
// [[Rcpp::depends("RcppArmadillo")]]
// [[Rcpp::export]]
arma::mat VtoMatCpp(int n,
arma::vec x) {
arma::mat V = arma::eye<arma::mat>(n,n) ;
V.elem(find(trimatu(V))) = x;
return(V);
}
当我在 R 中使用 sourceCpp('fun.cpp') 然后尝试 VtoMatCpp(2,1:3) 得到 Error: Mat::elem(): size mismatch。 trimatu 函数似乎没有选择对角线的索引。
【问题讨论】:
-
出于好奇,既然可以在 R 中本机完成此任务,为什么还要在 R 中的 C++ 中使用 Armadillo?
-
@AlexA。我没有尝试过替代方案,但感谢您的提示。到目前为止,我只是在学习
Rcpp,因为它似乎为非 C++ 程序员在从 R 中调用 C++ 时提供了一个“更简单”的接口。