说我有以下代码:
MatrixXd v(10, 10);
auto block = v.block(5, 0, 5, 2); // a block xpr to v
// view the block as vector
// in Eigen 3.4 it would be block.reshaped()
// but for Eigen 3.3, the following does not work:
Map<VectorXd>(block.data(), 10) = VectorXd::LinSpaced(10, 0, 1.);
// or the other way around, which errors on the constness
block = Map<MatrixXd>(VectorXd::LinSpaced(10, 0., 1.).eval().data(), 5, 2);
我的问题是,有没有一个很好的方法来实现这个与Eigen 3.3?
使用临时的“另一种方式”会做,所有你需要做的就是使用Map<const MatrixXd>
或静态方法MatrixXd::Map
来尊重constness:
block = Map<const MatrixXd>(VectorXd::LinSpaced(10, 0., 1.).eval().data(), 5, 2);
block = MatrixXd::Map(VectorXd::LinSpaced(10, 0., 1.).eval().data(), 5, 2);