2016-08-01 48 views
0

我无法重新分配Block。在下面的代码我的矩阵A存储在两种不同的方式:如何更改特征块?

  1. 为3 ArrayXd S,一个用于每个行
  2. 作为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>

相关问题:Passing a reference of a vector element to a threaded function

+0

你想避免使用'Eigen :: Map'吗? –

+0

@AviGinsburg,并不特别。当我完成时,我正在考虑使用它将'std :: vector '转换为'ArrayXXd'。你有什么想法? – user357269

+0

为什么首先要有一个'std :: vector '? –

回答

0

你并不需要一个Block<...>引用一行。你只需要一个索引。

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

+0

这个,而不是重新指派current_row_block指向第二行,覆盖第一行 – user357269

+0

@ user357269我的坏。我忘记了operator =已经超载。 – kangshiyin

+0

我实际上有几个具有类似行为的类,其中一部分功能是具有'current_row'句柄。在其中一个类中,这是一个'ArrayXd&',在另一个类中,它将是'Block '。产生current_row的内部工作是隐藏的,但它的行为像一个数组。 – user357269