简体   繁体   中英

Regular Multiplication of different shaped Eigen Matrices

I have an Nx3 Eigen matrix. I have an 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.

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:

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. 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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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