Search code examples
matrixvarianceeigen3

What is wrong in "a2 = a1-a1.mean();" to get centered data?


I am using Eigen 3.2.4 to obtain a centred data in a column vector.

Eigen::Matrix<double, 4, 1> a1, a2;
a1 << 1, 2, 3, 4;
a2 = a1 - a1.mean(); // error no match for operator -

But gcc is giving error as no match for operator -... What is the error here?


Solution

  • I am answering purely from the documentation, so I may be wrong but.

    Eigen does not allow Matrix - scalar, but does allow Array - scalar

    Try either.

    a2 = a1.array() - a1.mean();
    

    Or

    a2.array() = a1.array() - a1.mean();
    

    Even if neither of those work, hopefully they point you in the correct direction.