[英]Eigen - reshape a vector to matrix
I am trying to reshape a vector to a matrix, but getting the following error我正在尝试将向量重塑为矩阵,但出现以下错误
unsigned int Nx = 8;
unsigned int Ny = 7;
Eigen::VectorXi G_temp = Eigen::VectorXi::LinSpaced((Nx + 2) * (Ny + 2),0,(Nx + 2) * (Ny + 2)-1);
Eigen::MatrixXd G = Eigen::Map<Eigen::MatrixXd>(G_temp.data(),Nx+2, Ny+2); // error: no matching constructor for initialization of 'Eigen::Map<Eigen::MatrixXd>'
I followed what is written here , but I do not understand the way I am doing wrong.我遵循了这里写的内容,但我不明白我做错的方式。
There is no implicit conversion from integer-valued to double-valued expressions in Eigen.在 Eigen 中没有从整数值到双值表达式的隐式转换。 Either just use VectorXd
for G_temp
(and the LinSpaced
expression):要么只将VectorXd
用于G_temp
(和LinSpaced
表达式):
Eigen::VectorXd G_temp = Eigen::VectorXd::LinSpaced((Nx + 2) * (Ny + 2),0,(Nx + 2) * (Ny + 2)-1);
Or use a MatrixXi
-Map and .cast<double>()
the result before assigning it to G
.或者在将结果分配给G
之前使用MatrixXi
-Map 和 .cast .cast<double>()
结果。
Eigen::MatrixXd G = Eigen::Map<Eigen::MatrixXi>(G_temp.data(),Nx+2, Ny+2).cast<double>();
To avoid any temporary, you can also allocate a MatrixXd
and directly assign the proper values inside:为避免任何临时性,您还可以分配一个MatrixXd
并直接在其中分配适当的值:
Eigen::MatrixXd G(Nx+2, Ny+2); // allocate matrix
// set values in-place:
Eigen::VectorXd::Map(G.data(), (Nx + 2) * (Ny + 2)).setLinSpaced(0,(Nx + 2) * (Ny + 2)-1);
Or with the master/3.4 branch:或者使用 master/3.4 分支:
G.reshaped().setLinSpaced(0,(Nx + 2) * (Ny + 2)-1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.