【问题标题】:matlab sort one column and keep respective values on second columnmatlab对一列进行排序并将各自的值保留在第二列
【发布时间】:2012-08-03 03:05:10
【问题描述】:

我如何在 matlab 中做一个简单的排序。我总是必须使用 excel 链接来导入我的数据,对其进行排序,然后导出回 matlab。这很烦人!!!

我有一个矩阵 ,我想按降序对第一列进行排序,同时将其各自的值保留在第二列。 Matlab 似乎只是单独对每一列进行排序。

Example:
matrix a
5 4
8 9
0 6
7 3

matrix b (output)
0 6
5 4
7 3
8 9

【问题讨论】:

    标签: matlab sorting row


    【解决方案1】:

    @chaohuang 的sortrows 答案可能就是您要找的。但是,它会根据所有列进行排序。如果您只想根据第一列进行排序,那么您可以这样做:

    % sort only the first column, return indices of the sort
    [~,sorted_inds] = sort( a(:,1) );
    
    % reorder the rows based on the sorted indices
    b = a(sorted_inds,:); 
    

    【讨论】:

      【解决方案2】:

      只需使用b=sortrows(a);here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-09-16
        • 2017-03-26
        • 2020-12-24
        • 1970-01-01
        • 2017-02-17
        • 2012-06-29
        • 2021-05-03
        相关资源
        最近更新 更多