title: passing sparse arrays from matlab to Eigen (C++) and back to matlab?
body: <p>The following is a mex code that multiplies dense arrays g and G from matlab using Eigen. How do I do this when g is sparse?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;Eigen/Dense&gt;
  #include "mex.h"
  using Eigen::MatrixXd;
  using namespace Eigen;
  /*gateway function*/
  void mexFunction( int nlhs, mxArray *plhs[],
          int nrhs, const mxArray *prhs[]) {
  
      int nRows=(int)mxGetM(prhs[0]);
      int nCols=nRows;
  
      double* g=mxGetPr(prhs[0]);
      double* Gr=mxGetPr(prhs[1]);
  
      Map&lt;MatrixXd&gt; gmap (g, nRows, nCols );
      Map&lt;MatrixXd&gt; Grmap (Gr, nRows, nCols );
      plhs[0] = mxCreateDoubleMatrix(nRows, nCols, mxREAL);
      Map&lt;MatrixXd&gt; resultmap (mxGetPr(plhs[0]), nRows, nCols); 
  
      resultmap = gmap*Grmap; 
  
  }
  </code></pre>