简体   繁体   English

特征库,将矩阵的列乘以方矩阵

[英]Eigen library, multiply columns of matrix by square matrix

I want to transform each column of a matrix M by an operator N. Eigen allows to express this in terms of pre-multiplication: 我想通过运算符N变换矩阵M的每一列。Eigen允许用预乘法来表示:

M.colwise() *= N;

But the multiplication M_j * N is mathematically undefined. 但是乘法M_j * N在数学上是不确定的。

Is there some way to avoid writing a loop? 有什么方法可以避免编写循环吗?

If you want to multiply each column of M by N from the left, just perform a normal matrix-matrix-multiplication: 如果要将M的每一列从左边乘以N ,只需执行常规的矩阵-矩阵乘法:

M = N * M;

This will evaluate N*M into a temporary which is then moved to M . 这会将N*M评估为一个临时变量,然后将其移至M If you do this a lot and want to re-use the allocated memory for that, declare a temporary matrix M_temp somewhere before and write 如果您经常这样做,并且想为此重新使用分配的内存,请在之前的某个位置声明一个临时矩阵M_temp并编写

M_temp.noalias() = N * M;
M.swap(M_temp);  // M_temp has the old memory of M; Swapping is O(1)

If you are afraid of too much memory consumption, you can write something like 如果您担心过多的内存消耗,可以编写类似

for(long i=0; i<M.cols()-3; i+=4)
    M.middleCols<4>(i) = N * M.middleCols<4>(i);
M.rightCols(M.cols()%4) = N * M.rightCols(M.cols()%4);

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

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