给定两个动态 R x C 矩阵,如何交错行以生成一个 2R x C 矩阵?

发布于 2024-12-22 10:37:27 字数 753 浏览 3 评论 0原文

给定矩阵 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾铮苏瑾 2024-12-29 10:37:27

将每个 R x C 矩阵乘以由相应对角线上的 0 和 1 组成的 2R x R 矩阵,然后相加。

矩阵 1

1 0 0 0 ...
0 0 0 0 ...
0 1 0 0 ...
0 0 0 0 ...

矩阵 2

0 0 0 0 ...
1 0 0 0 ...
0 0 0 0 ...
0 1 0 0 ...

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

1 0 0 0 ...
0 0 0 0 ...
0 1 0 0 ...
0 0 0 0 ...

Matrix 2

0 0 0 0 ...
1 0 0 0 ...
0 0 0 0 ...
0 1 0 0 ...
泡沫很甜 2024-12-29 10:37:27

不确定这是否特定于 Eigen3,但您可以使用 MapStride 对象交错行。

MatrixXi C(A.rows()+B.rows(),A.cols());
Map<MatrixXi,0,Stride<Dynamic,2> >(C.data(),A.rows(),A.cols(),Stride<Dynamic,2>(2*A.rows(),2)) = A;
Map<MatrixXi,0,Stride<Dynamic,2> >(C.data()+1,B.rows(),B.cols(),Stride<Dynamic,2>(2*B.rows(),2)) = B;

来源

Not sure if this is specific to Eigen3, but you can interleave rows using the Map and Stride objects.

MatrixXi C(A.rows()+B.rows(),A.cols());
Map<MatrixXi,0,Stride<Dynamic,2> >(C.data(),A.rows(),A.cols(),Stride<Dynamic,2>(2*A.rows(),2)) = A;
Map<MatrixXi,0,Stride<Dynamic,2> >(C.data()+1,B.rows(),B.cols(),Stride<Dynamic,2>(2*B.rows(),2)) = B;

source

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文