matlab排序2列:第1列降序,第2列升序 [英] matlab sort 2 columns: column 1 descending and column 2 ascending

查看:417
本文介绍了matlab排序2列:第1列降序,第2列升序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个3列的数组,像这样:

I have a 3 column array like this:

A = [6 -1 0;
     6  0 3;
     1  4 3;
     1  2 5];

我需要第一列按降序排列,第二列按升序排列,第三列按其他两列(即它们未排序)的方式更改,如下所示:

I need the first column to be in descending order and the second column to be in ascending order with the third column changing with the other two columns (i.e. they are not sorted) like this:

B = [6 -1 0;
     6  0 3;
     1  2 5;
     1  4 3];

推荐答案

如果要保留行作为记录的完整性,并根据第1列和第2列对它们进行排序,请使用sortrows()(带负数列)索引以指示其值将按降序排序的列.

If you're looking to keep the rows intact as records, and sort them based on column 1 and then column 2, use sortrows(), with negative column indexes to indicate columns whose values are to be sorted in descending order.

B = sortrows(A, [-1 2])

这篇关于matlab排序2列:第1列降序,第2列升序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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