排序行是否始终保留排序组中的原始顺序? [英] Does sortrows always preserve the original ordering within sorting groups?

查看:66
本文介绍了排序行是否始终保留排序组中的原始顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MATLAB的 sortrows 函数似乎在每个排序组中的排序保持不变.有谁知道这是否真的正确,因为我找不到任何支持此要求的文档.

MATLAB's sortrows function seems to leave ordering unchanged within each sorting group. Does anyone know whether this is actually true, as I cannot find any documentation supporting this.

使用MATLAB提供的 sortrows 的示例:

Using MATLAB's provided example for sortrows:

A = {'Germany' 'Lukas'; 'USA' 'William'; 'USA' 'Andrew'; ...
'Germany' 'Andreas'; 'USA' 'Olivia'; 'Germany' 'Julia'} 

A = 

    'Germany'    'Lukas'  
    'USA'        'William'
    'USA'        'Andrew' 
    'Germany'    'Andreas'
    'USA'        'Olivia' 
    'Germany'    'Julia'  

并应用 sortrows(A,[1])

ans = 

    'Germany'    'Lukas'  
    'Germany'    'Andreas'
    'Germany'    'Julia'  
    'USA'        'William'
    'USA'        'Andrew' 
    'USA'        'Olivia' 

看到在原始数据中,第一列中的德国后面是 Lukas Andreas Julia 在第二列中从上至下读取.这将保留在最终结果中.

see that in the original data, Germany in the first column is followed by Lukas, Andreas, Julia in the second column reading from top to bottom. This is preserved in the end result.

这种行为能得到保证吗?

Is this behaviour guaranteed?

推荐答案

是.

Matlab使用

Matlab uses quick sort which is stable (if they don't employ some data specific optimizations). For the most part, I'd assume that Matlab uses stable sort, but cannot be sure without looking at the source.

您最好的选择是与Matlab技术支持联系,以确保其排序算法确实稳定,以及是否有计划在将来进行更改.

Your best bet is contacting Matlab tech support to make sure that their sort algorithm is indeed stable, and if they have any plans to change that in the future.

sortrows 使用内部排序.您可以阅读源代码:

sortrows uses the internal sort. You can read the source:

>> edit sortrows

edit:更多信息

这篇关于排序行是否始终保留排序组中的原始顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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