你如何改变Eigen中的Block?

时间:2016-08-01 18:38:02

标签: c++ eigen

我无法重新分配Block。在下面的代码中,我以两种不同的方式存储矩阵A

  1. 为3 ArrayXd s,每行一个
  2. 作为ArrayXXd
  3. // 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

1 个答案:

答案 0 :(得分: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