如何在MATLAB中针对第二行对二维数组进行排序? [英] How can I sort a 2-D array in MATLAB with respect to 2nd row?

查看:505
本文介绍了如何在MATLAB中针对第二行对二维数组进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组说"a"

I have array say "a"

a =

 1     4     5
 6     7     2

如果我使用功能 b = sort(a)

if i use function b=sort(a)

给出答案

b =

 1     4     2
 6     7     5

但是我想要像

b =

 5     1     4
 2     6     7

平均第二行应进行排序,但ist行的元素应保持不变,并应与第二行相对应.

mean 2nd row should be sorted but elements of ist row should remain unchanged and should be correspondent to row 2nd.

推荐答案

sortrows(a',2)'

sortrows(a',2)'

将其拆开:

a =  1     4     5
     6     7     2

a' = 1 6
     4 7
     5 2

sortrows(a',2) = 5 2
                 1 6
                 4 7

sortrows(a',2)' = 5 1 4
                  2 6 7

此处的关键是按指定的行对排序行进行排序,其他所有行均遵循其顺序.

The key here is sortrows sorts by a specified row, all the others follow its order.

这篇关于如何在MATLAB中针对第二行对二维数组进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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