数学建模社区-数学中国
标题:
[转载]Matlab函数sortrows:按升序排列行
[打印本页]
作者:
西北狼666
时间:
2016-3-29 17:51
标题:
[转载]Matlab函数sortrows:按升序排列行
功能:这一函数实现了类似Excel的按某一列排序各行数据的功能,各行数据作为一个整体来移动。
用法:
B = sortrows(A)
B = sortrows(A, column)
第一种和第二种用法的区别在于,sortrows(A)将类似按照字典序排列,而指定了column时,各行只根据指定列为标准来排序,不考虑其他列的顺序问题。
[B, index] = sortrows(A)
这一用法同时返回一个索引向量。如果A是一个雷向量,着B = A(index);如果A是一个m*n的数组,则B = A(index,
。
用例:
1. 字符串例
A = ['one ';'two ';'three';'four ';'five '];
B = sortrows(A)
C = sortrows(A,1)
复制代码
运行结果如下:
B =
five
four
one
three
two
C =
four
five
one
two
three
2. 数组例
A = magic(3)
B = sortrows(A)
C = sortrows(A)
[D, I] = sortrows(A)
复制代码
运行结果如下:
A =
8 1 6
3 5 7
4 9 2
B =
3 5 7
4 9 2
8 1 6
C =
3 5 7
4 9 2
8 1 6
D =
3 5 7
4 9 2
8 1 6
I =
2
3
1
3. 单列向量例
A = [8; 4; 7]
[B, I] = sortrows(A)
复制代码
结果如下:
A =
8
4
7
B =
4
7
8
I =
2
3
1
作者:
alcu123
时间:
2017-4-23 00:00
66666666666666666666666666666666
欢迎光临 数学建模社区-数学中国 (http://www.madio.net/)
Powered by Discuz! X2.5