将二维数组映射到Eigen :: Matrix [英] Map two-dimensional array to Eigen::Matrix

查看:658
本文介绍了将二维数组映射到Eigen :: Matrix的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道是否以及如何将二维双精度数组映射到Eigen :: Matrix。
是否可以映射 double d [] [] 数组,我将其作为 double ** p 到Eigen :: Matrix?

I can't figure out if and how it's possible to map a two-dimensional double array to an Eigen::Matrix. Is it possible to map an array double d[][] which I receive as double** p to an Eigen::Matrix?

虽然一维数组可以正常工作,但我无法映射 p Eigen :: Map< Eigen :: Matrix< double,n,n> 。那有可能吗,怎么办呢?大小 n 并不是真正恒定的,但我可以接受硬编码的大小。

While one-dimensinal arrays work fine, I wasn't able to map p to Eigen::Map<Eigen::Matrix<double, n, n>>. Is that possible and how could it be done? The size n is not really constant, but I could accept a hard coded size.

我尝试了多个版本,但没有一个起作用。我认为以下方法应该有效(假设大小 n 为4)。

I tried several versions, but none worked. I thought the following should work (assume the size n would be 4).

Eigen::Map<Eigen::Matrix<double, 4, 4>> p_OUTPUT(&p[0][0]);

代码可以编译并运行,但仅第一列的元素和第二列的第一元素列映射正确的值。使用 p [0] 作为参数会产生相同的结果。
我尝试过的其他版本(例如,没有& 的版本)没有编译。

The code compiles and runs, but only the elements of the first column and the first element of the second column map the correct values. Using p[0] as argument yields the same result. Other versions I tried (for example without the &) did not compile.

推荐答案

出于完整性考虑,我找到了一个解决方案。如前所述此处在此在不连续的内存中存储是个问题。

For the sake of completeness, I found a solution. As mentioned here or here the storage in not contiguous memory is the problem.

以下解决方案对我有用。

The following solution worked for me.

Eigen::MatrixXd ConvertToEigenMatrix(std::vector<std::vector<double>> data)
{
    Eigen::MatrixXd eMatrix(data.size(), data[0].size());
    for (int i = 0; i < data.size(); ++i)
        eMatrix.row(i) = Eigen::VectorXd::Map(&data[i][0], data[0].size());
    return eMatrix;
}

这篇关于将二维数组映射到Eigen :: Matrix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆