简体   繁体   English

不同形状的本征矩阵的正则乘法

[英]Regular Multiplication of different shaped Eigen Matrices

I have an Nx3 Eigen matrix. 我有一个Nx3特征矩阵。 I have an Nx1 Egein marix. 我有一个Nx1 Egein marix。 I'm trying to get the coefficient multiplication of each row in the Nx3 by the corresponding scal in the Nx1 so I can scale a bunch of 3d vectors. 我试图将Nx3中每一行的系数乘以Nx1中相应的标度,以便可以缩放一堆3d向量。

I'm sure I'm overlooking something obvious but I can't get it to work. 我敢肯定,我忽略了一些显而易见的事情,但我无法使其正常工作。

#include <Eigen/Dense>

MatrixXf m(4, 3);
m << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12;
MatrixXf dots(4, 1)
dots << 2,2,2,2;

I want to resulting matrix to be Nx3 like so: 我想像这样使矩阵为Nx3:

2,4,6
8,10,12,
14,16,18,
20,22,24

You can use broadcasting: 您可以使用广播:

m = m.colwise().cwiseProduct(dots);

or observe that all you want to do is to apply a non uniform scaling: 或观察到您要做的就是应用非均匀缩放:

m = dots.asDiagonal() * m;

Both expressions will generate similar code. 这两个表达式将生成相似的代码。

Okay, so I got something working. 好吧,我有工作了。 I'm probably doing something wrong but this worked for me so I thought I would share. 我可能做错了什么,但这对我有用,所以我想我会分享。 I wrote my first line of c++ a week ago so I figure I deserve some grace. 一周前,我写了第一行C ++,因此我觉得自己应该得到一些宽限。 Anyone with a better solution is encouraged to post. 鼓励具有更好解决方案的任何人发布。

// scalar/coefficient multiplication (not matrix) on Nx3 x N. For multiplying dot products by vectors
void N3xNcoefIP(MatrixXf &A, MatrixXf &B) {
    A.array() *= B.replicate(1, A.size()).array();
}

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

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