c++ - 使用 MatrixXf 的 Rcpp 特征映射错误

标签 c++ r eigen rcpp

为什么下面的代码编译不通过?

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/

相关文章:

c++ - 复数矩阵乘法 Eigen 与 Matlab

c# - 在 UI 线程中从 c++ 回调到 vb6

c++ - Visual Studio : how to get output in "Win32" project

r - ..1 和 ..2 在 R 中代表什么?

r - Shiny 中的 tryCatch 错误处理

r - 使用 install.packages 安装旧包

c++ - 如何在不指定维度的情况下用 Eigen 声明张量?

c++ - LNK2019 : Unresolved external symbol

c++ - VS2005中包含头文件

c++ - 如何在列中打印特征变量?