首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >矩阵向量cwiseProduct操作的Eigen3 replicate()

矩阵向量cwiseProduct操作的Eigen3 replicate()
EN

Stack Overflow用户
提问于 2014-08-26 03:08:20
回答 2查看 1.6K关注 0票数 4

我有以下代码:

代码语言:javascript
复制
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;

以下哪项输出:

代码语言:javascript
复制
3 0 5 0 7
0 4 0 6 0
3 4 5 6 7

有比使用replicate()调用更有效的方法来实现这一点吗?

EN

回答 2

Stack Overflow用户

发布于 2014-08-26 03:59:17

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

这些是等效的:

代码语言:javascript
复制
aMatrix.cwiseProduct( aVector.replicate( 1, aMatrix.rows() ).transpose() )
aMatrix.array().rowwise() * aVector.array().transpose()
票数 5
EN

Stack Overflow用户

发布于 2014-09-07 10:02:41

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

aMatrix * aVector.asDiagonal();

代码语言:javascript
复制
#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;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25492785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档