Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Eigen MatrixXf subtract a VectorXf

I have a Eigen::MatrixXf mat that has size of 113(rows) X 2009(cols); I am trying to subtract the max of each of its column from each of its column. Here is my code:

VectorXf minVal = mat.colwise().minCoeff();  // mat is MatrixXf
mat.colwise() -= minVal;

Here is the error message:

Assertion failed: dst.rows() == src.rows() && dst.cols() ==
src.cols(), file
C:\cui\Projects\eigen-3.4.0\Eigen\src\Core\AssignEvaluator.h, line 754

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Can anyone give a pointer? Thanks a lot.

>Solution :

mat.colwise().minCoeff() returns a row vector (you can assign this to a column vector, because in most cases Eigen implicitly transposes row vectors to column vectors and vice versa).
You should store the result as a Eigen::RowVectorXf and then subtract that from each row, i.e., mat.rowwise() -= minVal;

This should work:

Eigen::RowVectorXf minVal = mat.colwise().minCoeff();

std::cout << mat << "\n\n" << minVal << "\n\n";

mat.rowwise() -= minVal;

std::cout << mat << "\n\n";
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading