Friday, February 27, 2015

Eigen Library Customized cwise operation - Implementation the "sign()" function in Eigen/C++

In programming language like MATLAB, many coefficient wise operations are provided as function calls: such as sign(M).

For the version of Eigen I currently using, there doesn't seem to have a correspondent build-in implementation cwise operation.

However, a customized cwise operation can be achieve using unaryExpr.

Here is a simple example for achieving similar functionality as the sign() in MATLAB.


float sign(float x)
{
  if (x > 0)
    return 1.0;
  else
    return -1.0;
}



Mat.unaryExpr(std::ptr_fun(sign))

No comments:

Post a Comment