Multiplication of each matrix column by each vector element using Eigen C++ Library(使用 Eigen C++ 库将每个矩阵列与每个向量元素相乘)
问题描述
我需要使用 Eigen C++ 库 将每个矩阵列乘以每个向量元素.我试过 colwise 没有成功.
I need to multiply each matrix column by each vector element using Eigen C++ library. I tried colwise without success.
示例数据:
矩阵 A 可以有 3xN 和 V Nx1.含义 (cols x rowls).
Matrix A can have 3xN and V Nx1. Meaning (cols x rowls).
推荐答案
我会这样做:
示例输出:
说明
你很接近,诀窍是使用 .array()
来做广播乘法.
colwiseReturnType
没有 .array()
方法,所以我们必须在 A 的数组视图上做我们的 colwise 恶作剧.
colwiseReturnType
doesn't have a .array()
method, so we have to do our colwise shenanigans on the array view of A.
如果你想计算两个向量的元素乘积(最酷的酷猫称之为 Hadamard 产品),你可以做
If you want to compute the element-wise product of two vectors (The coolest of cool cats call this the Hadamard Product), you can do
以上代码以列方式执行的操作.
Which is what the above code is doing, in a columnwise fashion.
要解决行情况,您可以使用 .rowwise()
,并且您需要一个额外的 transpose()
以使事情适合
To address the row case, you can use .rowwise()
, and you'll need an extra transpose()
to make things fit
示例输出:
这篇关于使用 Eigen C++ 库将每个矩阵列与每个向量元素相乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!