Reputation: 3352
Using the Eigen3/C++ Library, given a MatrixXd
/ x0 ... y0 \
| x1 ... y1 |
M = | ... ... ... |
| |
\ xN ... yN /
what is the fasted method to achieve a modified version as shown below?
/ x0 * y0 ... y0 \
| x1 * y1 ... y1 |
M' = | ... ... ... |
| |
\ xN * yN ... yN /
That is, one column (the one with the x-s) is replaced by itself multiplied with another column (that with the y-s).
Upvotes: 0
Views: 122
Reputation: 5624
do you mean how to coefficient-wise assign-multiply the first and last column vectors ? there are many ways of doing it, but the easiest/fastest might be
Eigen::MatrixXd M2 = M;
M2.leftCols<1>().array() *= M2.rightCols<1>().array();
an alternative might be constructing an uninitialized matrix with a given number of rows/cols and then block-assign like
Eigen::MatrixXd M2{ M.rows(), M.cols() };
M2.rightCols( M.cols() - 1 ) = M.rightCols( M.cols() - 1 );
M2.leftCols<1>() = M.leftCols<1>().cwiseProduct( M.rightCols<1>() );
which is faster I don't know ( but your preferred profiler does ).
for future questions, here is the official Eigen quick reference ;)
Upvotes: 1