为了账号安全,请及时绑定邮箱和手机立即绑定

请问matlab该如何用sort函数实现sortrows的功能?

请问matlab该如何用sort函数实现sortrows的功能?

LEATH 2019-07-09 11:07:02
matlab 如何用sort函数实现sortrows的功能
查看完整描述

5 回答

?
PIPIONE

TA贡献1829条经验 获得超9个赞

a=rand(100,100); tic [b,pos]=sort(a(:,1)); aa=a(pos,:); toc tic A=sortrows(a,1); toc det(aa-A) 前面的算法时间要短,效果是一样的 Elapsed time is 0.000110 seconds. Elapsed time is 0.000259 seconds. ans = 0 用个小矩阵检测下 a=magic(5); tic [b,pos]=sort(a(:,1)); aa=a(pos,:) toc tic A=sortrows(a,1) toc det(aa-A) a = 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 aa = 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 17 24 1 8 15 23 5 7 14 16 Elapsed time is 0.000133 seconds. A = 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 17 24 1 8 15 23 5 7 14 16 Elapsed time is 0.000223 seconds. ans = 0

查看完整回答
1 反对 回复 2019-07-13
?
翻翻过去那场雪

TA贡献2065条经验 获得超13个赞

以下是自己按照程序帮助写的,没有copy,希望能帮助到你。
sortrows有三种用法:
B = sortrows(A)
B = sortrows(A,column)
[B,index] = sortrows(A,...)

我们先创建一个矩阵
A=floor(gallery('uniformdata',[6 7],0)*100);
A(1:4,1)=95; A(5:6,1)=76; A(2:4,2)=7; A(3,3)=73
A =
95 45 92 41 13 1 84
95 7 73 89 20 74 52
95 7 73 5 19 44 20
95 7 40 35 60 93 67
76 61 93 81 27 46 83
76 79 91 0 19 41 1

默认依据第一列的数值按升序移动每一行,如果第一列的数值有相同的,依次往右比较。例:
B = sortrows(A)
B =
76 61 93 81 27 46 83
76 79 91 0 19 41 1
95 7 40 35 60 93 67
95 7 73 5 19 44 20
95 7 73 89 20 74 52
95 45 92 41 13 1 84

或是从某一列开始比较数值并按升序排序,例:
C = sortrows(A,2)
C =
95 7 73 89 20 74 52
95 7 73 5 19 44 20
95 7 40 35 60 93 67
95 45 92 41 13 1 84
76 61 93 81 27 46 83
76 79 91 0 19 41 1

亦可以从某一列开始以降序排列,例:
D = sortrows(A, -4)
D =
95 7 73 89 20 74 52
76 61 93 81 27 46 83
95 45 92 41 13 1 84
95 7 40 35 60 93 67
95 7 73 5 19 44 20
76 79 91 0 19 41 1

如果要求每一列都按照升序排列
E=sort(A)

如果要求每一列都按照降序排列
F=-sort(-A)

查看完整回答
反对 回复 2019-07-13
?
繁花如伊

TA贡献2012条经验 获得超12个赞

SORTROWS Sort rows in ascending order.

它是按照行来做排序的

C =
2 2
2 1

就会变成

C =
2 1
2 2

它是把每一行当成一个数
譬如

0.4447 0.9218 0.4057
0.6154 0.7382 0.9355
0.7919 0.1763 0.9169

如果用sortrows排的话,它会把这个看成

444792184057
615473829355
791917639169

每一行中的一列相当于数的一位, 前面的权重大

如果需要每列升序排列,直接用sort即可

查看完整回答
反对 回复 2019-07-13
?
红糖糍粑

TA贡献1815条经验 获得超6个赞

先用for循环把矩阵的每一行提取出来成为一个单独的行向量,对提取出来的行向量进行求和,比较和值大小,然后根据和的大小从上到下依次将单独的行向量排列下去(还得用个for语句),构成一个新的矩阵,这个矩阵就是你所要求的了

查看完整回答
反对 回复 2019-07-13
?
慕的地10843

TA贡献1785条经验 获得超8个赞

sortrows的第二个参数可以指定按哪一列排序。

1234567SORTROWS(X,COL) sorts the matrix based on the columns specified in the    vector COL.  If an element of COL is positive, the corresponding column    in X will be sorted in ascending order; if an element of COL is negative,    the corresponding column in X will be sorted in descending order. For     example, SORTROWS(X,[2 -3]) sorts the rows of X first in ascending order     for the second column, and then by descending order for the third    column.

所以,对应的代码是:

1sortrows(A, 1)


查看完整回答
反对 回复 2019-07-13
  • 5 回答
  • 0 关注
  • 1364 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信