地图特征复制矩阵
Map Eigen replicate Matrix
我正在尝试将代码从 Matlab 导入到 C++。有一些与我的案例相关的信息 in the KDE Eigen Forums.
我想实现的是Matlab的meshgrid,那边给出的解决方案是
X = RowVectorXd::LinSpaced(1,3,3).replicate(5,1);
Y = VectorXd::LinSpaced(10,14,5).replicate(1,3);
即,.replicate
向量是另一个维度的量。在我的例子中,我有两个现有的 (n x 1) 个向量,并想创建一个 (n^2, 2) 矩阵,其中包含向量元素的所有组合,即:
[1 3 6]^T and [7 8]^T ==> [1 7, 3 7, 6 7, 1 8, 3 8, 6 8]^T
其中 ^T
仅表示转置,行以逗号分隔。 (在我的例子中,向量使用浮点数,但这无关紧要)。
矩阵 [1 3 6 1 3 6]^T
的第一列很容易由 Eigen 的 .replicate
函数创建。但是,我很难创建第二列 [7 7 7 8 8 8]^T
.
我的想法是在另一个维度(获得矩阵)中使用 .replicate
,然后使用 rowWise Eigen::Map
将其带到线性(矢量)视图(如建议的那样 in the docs),但我理解出现的编译器错误,例如 Eigen::Map
不适用于 Eigen::Replicate
类型。
#include <Eigen/Core>
using namespace Eigen;
int main()
{
MatrixXd reptest1(1, 5);
reptest1 << 1, 2, 3, 4, 5;
auto result2 = reptest1.replicate(2, 1); // cols, rows: 5, 2
auto result3 = Map<Matrix<double, 1, Dynamic, Eigen::RowMajor> >(result2); // this doesn't work
return 0;
}
VS2017 抱怨:error C2440: '<function-style-cast>': cannot convert from 'Eigen::Replicate<Derived,-1,-1>' to 'Eigen::Map<Eigen::Matrix<double,1,-1,1,1,-1>,0,Eigen::Stride<0,0>>'
海湾合作委员会也抱怨。 no matching function for call
(无法像在另一台机器上一样复制和粘贴准确的消息)。
我这样做是不是太复杂了?应该使用 Map 吗?
Map
只能作用于矩阵,不能作用于表达式。因此,将 auto result2
替换为 MatrixXd result2
,您就完成了。参见 common pitfalls。
我正在尝试将代码从 Matlab 导入到 C++。有一些与我的案例相关的信息 in the KDE Eigen Forums.
我想实现的是Matlab的meshgrid,那边给出的解决方案是
X = RowVectorXd::LinSpaced(1,3,3).replicate(5,1);
Y = VectorXd::LinSpaced(10,14,5).replicate(1,3);
即,.replicate
向量是另一个维度的量。在我的例子中,我有两个现有的 (n x 1) 个向量,并想创建一个 (n^2, 2) 矩阵,其中包含向量元素的所有组合,即:
[1 3 6]^T and [7 8]^T ==> [1 7, 3 7, 6 7, 1 8, 3 8, 6 8]^T
其中 ^T
仅表示转置,行以逗号分隔。 (在我的例子中,向量使用浮点数,但这无关紧要)。
矩阵 [1 3 6 1 3 6]^T
的第一列很容易由 Eigen 的 .replicate
函数创建。但是,我很难创建第二列 [7 7 7 8 8 8]^T
.
我的想法是在另一个维度(获得矩阵)中使用 .replicate
,然后使用 rowWise Eigen::Map
将其带到线性(矢量)视图(如建议的那样 in the docs),但我理解出现的编译器错误,例如 Eigen::Map
不适用于 Eigen::Replicate
类型。
#include <Eigen/Core>
using namespace Eigen;
int main()
{
MatrixXd reptest1(1, 5);
reptest1 << 1, 2, 3, 4, 5;
auto result2 = reptest1.replicate(2, 1); // cols, rows: 5, 2
auto result3 = Map<Matrix<double, 1, Dynamic, Eigen::RowMajor> >(result2); // this doesn't work
return 0;
}
VS2017 抱怨:error C2440: '<function-style-cast>': cannot convert from 'Eigen::Replicate<Derived,-1,-1>' to 'Eigen::Map<Eigen::Matrix<double,1,-1,1,1,-1>,0,Eigen::Stride<0,0>>'
海湾合作委员会也抱怨。 no matching function for call
(无法像在另一台机器上一样复制和粘贴准确的消息)。
我这样做是不是太复杂了?应该使用 Map 吗?
Map
只能作用于矩阵,不能作用于表达式。因此,将 auto result2
替换为 MatrixXd result2
,您就完成了。参见 common pitfalls。