本征垂直堆叠行到矩阵 [英] Eigen vertical stacking rows into Matrix

查看:97
本文介绍了本征垂直堆叠行到矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过垂直堆叠 2N 1x9创建一个大小为 2N x 9 的矩阵,其中 N 是动态值矩阵.

I would like to create a matrix of size 2N x 9 where N is a dynamic value by vertical stacking 2N 1x9 matrices.

这就是我尝试做的事情.

Here's what I tried doing.

using CoefficientMatrix = Eigen::Matrix<T, Eigen::Dynamic, 9>;
using CoefficientRow = Eigen::Matrix<T, 1, 9>;

CoefficientMatrix A(2*N, 9);

for (int i = 0; i < N; i++) {
    CoefficientRow ax;
    CoefficientRow ay;
    // fill in ax and ay
    A << ax, ay;
}

但是,出现以下运行时错误.

But, I get the following runtime error.

Assertion failed: (((m_row+m_currentBlockRows) == m_xpr.rows() || m_xpr.cols() == 0) && m_col == m_xpr.cols() && "Too few coefficients passed to comma initializer (operator<<)"), function finished, file /usr/local/include/eigen3/Eigen/src/Core/CommaInitializer.h, line 120.

我尝试通过断言语法进行解析,但是我不确定这些内部变量名在我的代码(Eigen的新手)中指的是什么.

I tried parsing through the assertion syntax but I'm not sure what those internal variable names are referring to in terms of my code (new to Eigen).

感谢您的帮助.

推荐答案

TLDR:编写如下内容:

TLDR: Write something like this:

CoefficientMatrix A(2*N, 9);

for (int i = 0; i < N; i++) {
    CoefficientRow ax;
    CoefficientRow ay;
    // fill in ax and ay
    A.row(2*i)   = ax;
    A.row(2*i+1) = ay;
}


出现错误的原因是(如Avi所解释的) operator<< 旨在立即填充整个矩阵.实际上,调用 operator<<(Array& A,Array const& b)会将 b 分配给 A ,然后返回一个代理对象,该对象保留对 A 的引用,并跟踪已分配给 A 的条目数(存储在 m_row , m_currentBlockRows m_col )并重载 operator ,该运算符将下一个表达式分配给 a 的相应位置,并且相应地增加位置.最后,当该代理对象遭到破坏(通常发生在"; "处)时,析构函数会检查 A 的所有条目是否都已填满(并引发失败的断言,如果没有).


The reason of your error is (as Avi explained) that the operator<< is intended to fill an entire matrix at once. In fact, calling operator<<(Array &A, Array const &b) assigns b to the top-left corner of A and returns a proxy-object which holds a reference to A and keeps track of how much entries of A already got assigned to (stored in m_row, m_currentBlockRows, m_col) and overloads operator, which assigns the next expression to the corresponding position of a and increases the position accordingly. Finally, when that proxy object gets destructed (which usually happens "at the ;") the destructor checks if all entries of A have been filled (and raises a failed assertion, if not).

如果您喜欢使用<<,您也可以编写以下语法:

If you prefer to use the << , syntax you could also write:

    A.middleRows<2>(2*i) << ax, ay;

启用优化后,该优化应生成与上述简单实现相同的代码(因此,请选择对您来说更容易理解的代码).

With optimizations enabled that should generate the same code as the simple implementation above (so choose whichever is easier to read for you).

NB:从技术上讲,您可以在循环中使用(ab)CommaInitializer ,方法是在循环外进行构造,将其分配给变量,然后仅使用循环内的运算符.我将故意不提供有关如何执行此操作的更多详细信息...

N.B.: You could technically (ab)use the CommaInitializer in a loop, by constructing it outside the loop, assigning it to a variable and then only use the , operator inside the loop. I'll intentionally won't give more details on how to do that ...

这篇关于本征垂直堆叠行到矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆