Matlab重新缩放命令的本征等效项 [英] Eigen equivalent of Matlab rescale command

查看:107
本文介绍了Matlab重新缩放命令的本征等效项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Eigen中重新创建Matlab重新缩放命令

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

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

我尝试翻译,但不确定。我的Eigen知识仍然太有限了……

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;
  };

这可行吗?

推荐答案

否。您的尺寸不匹配。您正在混合 ArrayXd ArrayXXd 。这更像您想要的,带有一个用于标量的版本和一个用于矢量的版本。调整逐行 / 逐行以匹配不同版本的matlab 重新缩放

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;
}

这篇关于Matlab重新缩放命令的本征等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆