简体   繁体   English

将矩阵列乘以 Eigen 中的向量

[英]Multiply matrix columns into a vector in Eigen

I would like to multiply the columns of matrix A to vector v and finally get the sum of each column.我想将矩阵 A 的列乘以向量 v 并最终得到每列的总和。 I use Eigen library and coded as follows:我使用 Eigen 库并编码如下:

for (int j = 0 ; j< A.cols(); j++)
    C.col(j).noalias() = V.cwiseProduct(A.col(j));
V2 = C.colwise().sum();

However, it is still slow.但是,它仍然很慢。 I also used the following products but these are even slower.我还使用了以下产品,但这些产品甚至更慢。

1. C = A * V.asDiagonal();
2. C = A.array().rowwise() * V.transpose().array();

Any idea?任何的想法?

Hm... what you're looking for is a plain standard vector-matrix product:嗯......你正在寻找的是一个普通的标准向量矩阵产品:

V2.noalias() = V.transpose() * A;

The .noalias() is optional, it's only here to save you one temporary. .noalias()是可选的,它只是在这里为您保存一个临时的。

If you really need the intermediate matrix C , then your two options should be equally fast as your hand-crafted for loop provided that you correct them to make them compute the same thing:如果你真的需要中间矩阵C ,那么你的两个选项应该和你手工制作的 for 循环一样快,前提是你更正它们以使它们计算相同的东西:

C = V.asDiagonal() * A;
C = A.array().colwise() * V.array();

And don't forget to enable compiler optimizations, eg -O3 -march=native .并且不要忘记启用编译器优化,例如-O3 -march=native

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM