我有以下代码:
Eigen::MatrixXf aMatrix( 3, 5 );
aMatrix <<
1, 0, 1, 0, 1,
0, 1, 0, 1, 0,
1, 1, 1, 1, 1;
Eigen::VectorXf aVector( 5 );
aVector << 3, 4, 5, 6, 7;
cout << aMatrix.cwiseProduct( aVector.replicate( 1, aMatrix.rows() ).transpose() ) << endl;以下哪项输出:
3 0 5 0 7
0 4 0 6 0
3 4 5 6 7有比使用replicate()调用更有效的方法来实现这一点吗?
发布于 2014-08-26 03:59:17
已解决(在How can I apply bsxfun like functionality at Eigen?的帮助下)
这些是等效的:
aMatrix.cwiseProduct( aVector.replicate( 1, aMatrix.rows() ).transpose() )
aMatrix.array().rowwise() * aVector.array().transpose()发布于 2014-09-07 10:02:41
我不确定这是否更有效,但后乘对角线矩阵是另一种选择。
aMatrix * aVector.asDiagonal();
#include <iostream>
#include <Eigen/Dense>
int main()
{
Eigen::MatrixXf aMatrix( 3, 5 );
aMatrix <<
1, 0, 1, 0, 1,
0, 1, 0, 1, 0,
1, 1, 1, 1, 1;
Eigen::VectorXf aVector( 5 );
aVector << 3, 4, 5, 6, 7;
std::cout << aMatrix * aVector.asDiagonal() << std::endl;
return 0;
}https://stackoverflow.com/questions/25492785
复制相似问题