Matlab-排序矩阵 [英] Matlab - sorting a matrix

查看:86
本文介绍了Matlab-排序矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我可以用一个例子来最好地说明这一点: 假设我有A = [1,4,2,3; 0,-1,2,-1]

I think I can illustrate this best with an example: Suppose I have A = [ 1, 4, 2, 3; 0,-1, 2, -1]

我想把它变成 [1、2、3、4; 0、2,-1,-1]

I want to turn it into [1, 2, 3, 4; 0, 2, -1, -1]

即保持列完整,并按第一行中的条目排序.我该怎么办?

i.e. keep columns intact, sort by entries in first row. How do I do this?

推荐答案

sortrows 命令可以满足您的要求:

The sortrows command does what you want:

>> A = [ 1, 4, 2, 3; 0,-1, 2, -1];
>> sortrows(A.').'   

ans =

     1     2     3     4
     0     2    -1    -1

您还可以使用sort中的第二个返回值来获取将矩阵转换为所需矩阵所必需的列置换:

You can also use the second return value from sort to get the column permutation necessary to turn your matrix into the one you want:

>> [~,ii] = sort(A(1,:))

ii =

     1     3     4     2

>> A(:,ii)

ans =

     1     2     3     4
     0     2    -1    -1

这篇关于Matlab-排序矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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