Eigen convert dense matrix to sparse one

Alberto picture Alberto · Oct 23, 2012 · Viewed 9.2k times · Source

How to convert an Eigen::Matrix<double,Dynamic,Dynamic> to an Eigen::SparseMatrix<double> ? I'm looking for a better way instead of iterate through the dense matrix

Answer

ggael picture ggael · Oct 24, 2012

you can use the sparseView() method for that:

sparse = dense.sparseView();

and even specify a tolerance:

sparse = dense.sparseView(epsilon,reference);