为什么下面的代码编译不通过?
library(Rcpp)
cppFunction('
int rows(const NumericMatrix& X) {
using Eigen::MatrixXf;
typedef Eigen::Map<MatrixXf> MapMat;
MapMat X1(as<MapMat>(X));
return X1.rows();
}', depends = "RcppEigen")
它抛出以下错误:
error: no matching function for call to 'Eigen::Map<Eigen::Matrix<float, -1, -1> >::Map(Rcpp::Vector<14, Rcpp::PreserveStorage>::iterator, int&, int&)'
OUT get() {return OUT(vec.begin(), d_nrow, d_ncol );}
当我改用 MatrixXd 时,相同的代码工作正常。
谢谢。
最佳答案
NumericMatrix
使用类型 double
(与 float
相对)。 Eigen 不支持使用不同类型的矩阵之间的隐式类型转换。您的代码似乎尝试将 double
NumericMatrix
的内存读取为 float
Eigen 矩阵。只需使用 MatrixXd
类型即可。
关于c++ - 使用 MatrixXf 的 Rcpp 特征映射错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40114103/