我们一直在使用sample
函数从RcppArmadillo
随机抽样一个NumericVector
对象。但是,我们注意到在犰狳类型上不可能使用相同的功能(vec
或uvec
)。我们已经看了sample.h
文件中的函数定义,它看起来像一个模板化的函数,应该能够处理这些类型,但是我们还没有能够弄清楚如何使它与Armadillo类一起工作而没有做很多来自Rcpp
库的NumericVector
或IntegerVector
类型的转换。RcppArmadillo样本上犰狳矢量类
例如,我们将此函数写入一个名为try.cpp
的文件中。
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
#include <RcppArmadilloExtensions/sample.h>
using namespace arma;
using namespace Rcpp;
// [[Rcpp::export]]
arma::uvec sample_index(const int &size){
arma::uvec sequence = linspace<uvec>(0, size-1, size);
arma::uvec out = sample(sequence, size, false);
return out;
}
运行上面的代码产生了以下错误:
src/try.cpp|11 col 22 error| no matching function for call to 'sample' [cpp/gcc]
~/Library/R/3.3/library/Rcpp/include/Rcpp/sugar/functions/sample.h|401 col 1 error| note: candidate function not viable: no known conversion from 'arma::uvec' (aka 'Col<unsigned int>') to 'int' for 1st argument [cpp/gcc]
~/Library/R/3.3/library/Rcpp/include/Rcpp/sugar/functions/sample.h|437 col 1 error| note: candidate template ignored: could not match 'Vector' against 'Col' [cpp/gcc]
任何帮助,将不胜感激:)
您能否快速确认RcppArmadillo的版本是最新的? 'sessionInfo()'> = 7.6 – coatless
'sessionInfo()'的输出:'RcppArmadillo_0.7.700.0.0' –
作为第一步(防御),撤销'using namespace ...',因为我们现在有_two_'sample'函数。 –