本征:按行复制(广播) [英] Eigen: Replicate (broadcast) by rows
问题描述
我想复制矩阵 M
的每一行而不会发生任何复制(即通过创建视图):
I'd like to replicate each row of a matrix M
without any copy occurring (i.e. by creating a view):
0 1 0 1
2 3 -> 0 1
2 3
2 3
-
M.rowwise()。replicate(n)
是M.replicate(1,n)
似乎没有用。以下代码段进行了复制,并且如果
M
不起作用The following snippet does a copy, and cannot work if
M
is an expression.Eigen::Index rowFactor = 2; Eigen::MatrixXi M2 = Eigen::Map(M.data(), 1, M.size()).replicate(rowFactor, 1); M2.resize(M.rows()*rowFactor, M.cols()) ;
- 在某些情况下,我可以使用中间视图
Eigen :: Map< Eigen :: MatrixXi>(M.data(),1,M.size())。replicate(rowFactor,1)
重塑其他操作数,但这不是很令人满意。 - In some situation, I may use the intermediate view
Eigen::Map<Eigen::MatrixXi>(M.data(), 1, M.size()).replicate(rowFactor, 1)
by reshaping the other operands, but that's not very satisfying.
是否有实现此广播视图的正确方法?
Is there a proper way to achieve this broadcast view?
推荐答案
您想要的本质上是一个带有一个矩阵的 Kronecker产品。您可以为此使用(不受支持的) KroneckerProduct模块:
What you want is essentially a Kronecker product with a matrix of ones. You can use the (unsupported) KroneckerProduct module for that:
#include <iostream> #include <unsupported/Eigen/KroneckerProduct> int main() { Eigen::Matrix2i M; M << 0, 1, 2, 3; std::cout << Eigen::kroneckerProduct(M, Eigen::Vector2i::Ones()) << '\n'; }
被不支持意味着模块的API不能保证是稳定(尽管我认为自引入以来该模块没有改变)。
Being 'unsupported' means that the API of the module is not guaranteed to be stable (though this module has not changed since its introduction, I think).
这篇关于本征:按行复制(广播)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 在某些情况下,我可以使用中间视图