'How to remove nan from a eigen matrix?

I am using eigen library.

The reason i used eigen is i want do linear regression in my c++ code.

y = W * x + b

x and y is my data, with format eigen matrix, there may be NAN in them.

but when my x or y contains NAN value, the linear regression will fail with w = [nan,....]

I want to avoid this, so i need to remove the nan line.

for example:

#include <eigen3/Eigen/Dense>
#include <iostream>

using namespace Eigen;
using namespace std;
int main() {
  VectorXd e = VectorXd::Random(5) * 3;
  cout << " e:" << endl << e << endl;

  VectorXd b = VectorXd::Random(2) * 18; 
  cout << " b:" << endl << b << endl;

  MatrixXd x = MatrixXd::Random(5, 2) * 100;
  cout << " x:" << endl << x << endl;

  MatrixXd y = x * b + e;
  cout << " y:" << endl << y << endl;

  y(3,0) = NAN;  // i made the third element of y as NAN, this will cause linear regression failed
  // what i want is, just ignore the 3rd line of y and x, then do linear regression

}

this is quite similar with python pandas operation:

df = pd.DataFrame(...)
df = df.dropna()

could you help on this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source