问题描述
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使用稳定的排序,但是如果不查看源代码就无法确定.
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:更多信息
这篇关于排序行是否始终保留排序组中的原始顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!