matlab中sort的函数怎么实现像excel中使一个MXN的矩阵按照第一列排序,然后其他列跟着变化?
展开全部
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
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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询