title: Eigen Matrix sum with NANs
body: <p>I have an Eigen Matrix A which includes NAN values. I want to get the sum of differences of this matrix to multiple other matrices.</p>
  
  <pre><code>double getDistance(const Eigen::MatrixXf&amp; from, const Eigen::MatrixXf&amp; to)
  {
      Eigen::MatrixXf difference = (to - from).cwiseAbs2();
  
      difference = difference.unaryExpr([](float v, double&amp; sum)
      { return std::isnan(v) ? 0.0f : v;});
  
      double distance = difference.sum();
  
      return distance;
  }
  
  std::vector&lt;double&gt; getDistances(const std::vector&lt;Eigen::MatrixXf&gt;&amp; from, const Eigen::MatrixXf&amp; to)
  {
      std::vector&lt;double&gt; distances;
      for (int i = 0; i &lt; from.size(); ++i)
      {
         distances.push_back(getDistance(from[i], to));
      }
      return distances;
  }
  </code></pre>
  
  <p>Right now I need to remove the NANs of <code>difference</code> every single time and then take the sum.
  I was thinking about doing my own sum function which skips NANs.<br>
  Is there an elegant way to do this?</p>
  
  <p>Does <code>unaryExpr</code> work for summing up where we need an "out parameter"?</p>