给定两个动态 R x C 矩阵,如何交错行以生成一个 2R x C 矩阵?
给定矩阵 A
a_0_0, a_0_1, a_0_2, ...
a_1_0, a_1_0, a_1_2, ...
...
和矩阵 B:
b_0_0, b_0_1, b_0_2, ...
b_1_0, b_1_1, b_1_2, ...
...
使用 eigen2,并 A 和 B 具有相同的维度,我想交错行,产生:
a_0_0, a_0_1, a_0_2, ...
b_0_0, b_0_1, b_0_2, ...
a_1_0, a_1_0, a_1_2, ...
b_1_0, b_1_1, b_1_2, ...
...
显然,我可以编写一个函数来构造适当维度的输出矩阵,然后循环每个输入矩阵并将元素分配给结果。不过,我不想重新发明轮子,所以如果 eigen2 已经有一种机制来优雅地表达这种矩阵手术,我更愿意使用它。
我确实浏览了 eigen2 文档,但我并没有发现任何明显正确的内容。我发现的最接近的是 MatrixBase::select,但它确实是“来自 a 的元素或来自 b 的元素”,其中我想要的是“来自下一行中 b 的元素”。
效率并不是最重要的问题,因为我不需要在快速路径中执行此操作,只需在初始化时执行此操作。
如果有更好的方法来表示矩阵,我对格式表示歉意。
Using eigen2, and given a matrix A
a_0_0, a_0_1, a_0_2, ...
a_1_0, a_1_0, a_1_2, ...
...
and a matrix B:
b_0_0, b_0_1, b_0_2, ...
b_1_0, b_1_1, b_1_2, ...
...
and where A and B have the same dimensions, I would like to interleave the rows, producing:
a_0_0, a_0_1, a_0_2, ...
b_0_0, b_0_1, b_0_2, ...
a_1_0, a_1_0, a_1_2, ...
b_1_0, b_1_1, b_1_2, ...
...
Obviously I can write a function that will construct an output matrix of the proper dimensions, then loop over each of the input matrices and assign elements to the result. I'd rather not re-invent the wheel though, so if eigen2 already has a mechanism to express this sort of matrix surgery elegantly I'd much prefer to use it.
I did look through the eigen2 docs and nothing jumped out at me as obviously correct. The closest thing I found was MatrixBase::select, but that does 'element from a or element from b', where what I want is 'element from a then element from b in the next row'.
Efficiency is not of paramount concern since I don't need to do this in the fast path, only at initialization.
I apologize for the formatting if there is a better way to represent matrices.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将每个 R x C 矩阵乘以由相应对角线上的 0 和 1 组成的 2R x R 矩阵,然后相加。
矩阵 1
矩阵 2
Multiply each R x C matrix by a 2R x R matrix consisting of zeroes and ones on the appropriate diagonal, then add.
Matrix 1
Matrix 2
不确定这是否特定于 Eigen3,但您可以使用
Map
和Stride
对象交错行。来源
Not sure if this is specific to Eigen3, but you can interleave rows using the
Map
andStride
objects.source