如何更改Eigen中的块? [英] How do you change a Block in Eigen?
本文介绍了如何更改Eigen中的块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法重新分配封锁
。在下面的代码中,我以两种不同的方式存储矩阵 A
:
I'm having trouble reassigning a Block
. In the code below I store the matrix A
in two different ways:
- 3
ArrayXd
s,每行一个 - 作为
ArrayXXd
li>
- as 3
ArrayXd
s, one for each row - as an
ArrayXXd
。
// data
ArrayXXd A (3, 3);
A << 0, 1, 2, 3, 4, 5, 6, 7, 8;
std::vector<ArrayXd> A_rows = {A.row(0), A.row(1), A.row(2)};
// std::vector<ArrayXd> solution
// first row
ArrayXd & current_row = A_rows[0];
// read it, write it, do stuff
// start working with the second row
current_row = std::ref(A_rows[1]);
cout << current_row << endl << endl; // prints 3 4 5
cout << A << endl; // A is unchanged
// Eigen solution
// first row
Block<ArrayXXd, 1, -1> && current_row_block = A.row(0);
// read it, write it, do stuff
// start working with the second row
current_row_block = std::ref(A.row(1)); // doesn't compile
cout << current_row_block << endl;
cout << A << endl;
错误讯息是:
error: use of deleted function 'void std::ref(const _Tp&&) [with _Tp = Eigen::Block<Eigen::Array<double, -1, -1>, 1, -1, false>]'
current_row_block = std::ref(A.row(1));
^
可以修复第二种方法, std :: vector< ArrayXd>
?
Is it possible to fix the second approach or should I move to storing the matrix as std::vector<ArrayXd>
?
相关问题:将向量元素的引用传递给线程函数
推荐答案
您不需要 Block< ...>
。您只需要一个索引。
You don't need a Block<...>
to reference a row. You only need an index.
int current_row_id = 0;
std::out << A.row(current_row_id) << std::end;
current_row_id = 1;
std::out << A.row(current_row_id) << std::end;
对于您的 std :: vector< ArrayXd>
方法,因为您是复制行,您不能更改原来的 A
。
For your std::vector<ArrayXd>
approach, as you are make copies of the rows, you can not change the original A
.
这篇关于如何更改Eigen中的块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文