1

I'd like to recreate the Matlab rescale command in Eigen

https://www.mathworks.com/help/matlab/ref/rescale.html

I've tried to translate but not sure about it. My Eigen knowledge is too limited still...

  auto rescale = [&](
    Eigen::MatrixXd mat, 
    Eigen::VectorXd inmin,
    Eigen::VectorXd inmax,
    Eigen::VectorXd l,
    Eigen::VectorXd u
    ) -> Eigen::MatrixXd {

    auto val = l.array() + (
      ((mat - inmin).array()) / ((
        ((inmax - inmin).array()) * ((u - l).array())
      ).array())
    );

    return val;
  };

could this be viable?

Kabu
  • 519
  • 6
  • 16
  • Sure it could be. Define your requirements for this function and write the tests to check if it behaves as you wish. In matlab docs the requirements for `rescale` are not that trivial. – pptaszni May 13 '19 at 16:27
  • The matlab rescale command is here https://www.mathworks.com/matlabcentral/answers/196798-how-to-normalize-values-in-a-matrix-to-be-between-0-and-1 – Kabu May 14 '19 at 19:37

1 Answers1

2

No. Your dimensions don't match. You're mixing ArrayXd and ArrayXXd. This is more like what you want, with a version for scalars and another for vectors. Adjust the rowwise/colwise to match the different versions of matlabs rescale.

#include <Eigen/Core>
#include <iostream>

int main()
{

    auto rescale = [&](
        Eigen::MatrixXd mat,
        double l,
        double u
        ) -> Eigen::MatrixXd {

        double min = mat.minCoeff();
        double max = mat.maxCoeff();
        auto val = l + (mat.array() - min) * ((u - l) / (max - min));
        return val;
    };

    Eigen::MatrixXd mat(4,4);
    Eigen::Map<Eigen::VectorXd>(mat.data(), mat.size()).setLinSpaced(1, mat.size());

    std::cout << mat << "\n\n";

    auto rescaled = rescale(mat, 2, 5);

    std::cout << rescaled << "\n\n";

    auto rescale2 = [&](
        Eigen::MatrixXd mat,
        Eigen::VectorXd l,
        Eigen::VectorXd u
        ) -> Eigen::MatrixXd {

        Eigen::ArrayXd  min = mat.colwise().minCoeff();
        Eigen::ArrayXd  max = mat.colwise().maxCoeff();
        auto val = l.array().replicate(1, mat.cols())
            + ((mat.array().rowwise() - min.transpose()).rowwise() * 
               ((u - l).array() / (max - min)).transpose());
        return val;
    };

    Eigen::VectorXd mn, mx;

    mn.resize(mat.cols());
    mx.resize(mat.cols());

    mn.setConstant(1.3);
    mx << 2, 5, 6, 9;
    rescaled = rescale2(mat, mn, mx);

    std::cout << rescaled << "\n\n";



    return 0;
}
Avi Ginsburg
  • 10,323
  • 3
  • 29
  • 56