码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Eigen::Matrix 排序


    在做 matlab 转C++代码时, 需要对 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

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

  • 相关阅读:
    python 调用adb shell
    FifoBuffer函数库详解
    [基于瑞芯微RV1126调试RTL8818FU WIFI模组支持STA和AP模式]
    跨境电商必读:如何选择适合跨境ERP系统?
    设计模式——抽象工厂模式(Abstract Factory Pattern)+ Spring相关源码
    git merge本地合并分支出现文件冲突处理方法
    Python 编写确定个位、十位以上方法及各数位的和程序
    我的 ReactNative 开发利器: 常用命令总结
    数据分析Pandas专栏---第五章<Pandas缺失值的处理(1)>
    jquery中 offset()计算的偏移量 和 原生Dom计算的偏移量不一致;
  • 原文地址:https://blog.csdn.net/falwat/article/details/126573340
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号