Matrix = Matrix.rowwise()。reverse()特征行反转矩阵并将其赋值回使每行对称

问题描述 投票:2回答:1

简单的问题,但无法解释原因:

输入

Eigen::MatrixXd Ha(2, 3);
Ha << 1, 2, 3, 4, 5, 6;
std::cout << "Ha: " << std::endl << Ha << std::endl;

Ha.rowwise().reverse();
std::cout << "Ha: " << std::endl << Ha.rowwise().reverse() << std::endl;

Ha = Ha.rowwise().reverse();
std::cout << "Ha: " << std::endl << Ha.rowwise().reverse() << std::endl;

产量

Ha:
1 2 3
4 5 6
Ha:
3 2 1
6 5 4
Ha:
3 2 3
6 5 6

对于最后一个,为什么会这样?

c++ matrix eigen
1个回答
5
投票

好吧,我终于明白了。这是因为Eigen使用惰性求值,我们需要覆盖对元素运算的惰性求值。

这样可行:

Ha = Ha.rowwise().reverse().eval();

这在Eigen的wiki中被称为“Aliasing”:

https://eigen.tuxfamily.org/dox/group__TopicAliasing.html

© www.soinside.com 2019 - 2024. All rights reserved.