【问题标题】:Eigen3 replicate() for a matrix-vector cwiseProduct operation矩阵向量 cwiseProduct 操作的 Eigen3 replicate()
【发布时间】:2014-08-25 19:08:19
【问题描述】:

我有以下代码:

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() 调用更有效的方法来实现这一点?

【问题讨论】:

    标签: c++ matrix-multiplication eigen eigen3


    【解决方案1】:

    已解决(在 How can I apply bsxfun like functionality at Eigen? 的帮助下)

    这些是等价的:

    aMatrix.cwiseProduct( aVector.replicate( 1, aMatrix.rows() ).transpose() )
    aMatrix.array().rowwise() * aVector.array().transpose()
    

    【讨论】:

      【解决方案2】:

      我不确定这是否更有效,但后乘以对角矩阵是另一种选择。

      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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-25
        • 1970-01-01
        • 1970-01-01
        • 2023-03-23
        • 2015-03-24
        • 1970-01-01
        • 1970-01-01
        • 2013-07-10
        相关资源
        最近更新 更多