• Eigen::Matrix 排序


    在做 matlabC++代码时, 需要对 Eigen::MatrixXd矩阵进行排序, 但未找到排序函数(也可能我找的不够仔细, 如果您找到了请留言告诉我!!), 所以自己写了一个对Eigen::MatrixX模板类进行排序的函数, 希望对大家有帮助!
    模板函数请放置到头文件中.

    /**
     * @brief 对矩阵 x 按行(或列)进行排序.
     * 
     * @tparam _Scalar 
     * @param x 待排序的矩阵
     * @param dim 0: 按行排序, 1: 按列排序
     * @param ascend true: 升序, false: 降序
     * @return Eigen::MatrixX<_Scalar> 排序后的矩阵
     */
    template <typename _Scalar>
    Eigen::MatrixX<_Scalar> sort(const Eigen::MatrixX<_Scalar> &x, int dim = 1, bool ascend = true)
    {
        Eigen::MatrixX<_Scalar> y(x);
        auto pred_ascend = [](double a0, double a1){return a0 < a1;};
        auto pred_descend = [](double a0, double a1){return a0 > a1;};
    
        if(dim == 0)
        {
            if(ascend)
            {
                for(auto row: y.rowwise())
                {
                    std::sort(row.begin(), row.end(), pred_ascend);
                }
            }
            else
            {
                for(auto row: y.rowwise())
                {
                    std::sort(row.begin(), row.end(), pred_descend);
                }
            }
        }
        else if(dim == 1 || dim == -1)
        {
            if(ascend)
            {
                for(auto col : y.colwise())
                {
                    std::sort(col.begin(), col.end(), pred_ascend);
                }
            }
            else
            {
                for(auto col : y.colwise())
                {
                    std::sort(col.begin(), col.end(), pred_descend);
                }
            }
        }
        return y;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    以下是从测试代码:

    #include 
    #include 
    #include "xxxx.h" // sort()模板函数定义在这里
    
    int main(int, char**) {
        Eigen::MatrixXd x(3,3);
        x << 2, 9, 4,
             7, 5, 3,
             6, 1, 8;
    
        std::cout << "x ascend sort by row: \n" << sort(x, 0) << std::endl;
        std::cout << "x ascend sort by col: \n" << sort(x, 1) << std::endl;
        std::cout << "x descend sort by col: \n" << sort(x, 1, false) << std::endl;
        std::cout << "x descend sort by row: \n" << sort(x, 0, false) << std::endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    测试结果如下:
    在这里插入图片描述

  • 相关阅读:
    ARM32开发--RTC内置实时时钟
    DRF 视图组件
    vue3 的动态路由 以及history模式
    delphi fmx zxing原生不使用外部库二维码,条码扫描速度很快
    用python实现自动回复QQ消息——不到60行
    使用Qt模仿文字浮动字母
    AI:06-基于OpenCV的二维码识别技术的研究
    Windows任务计划程序Task Scheduler笔记
    牛客刷题,python入门基础(11)
    物联网IOT发展智慧农业 助力乡村振兴
  • 原文地址:https://blog.csdn.net/falwat/article/details/126573340