Matlab-根据模式对单元格数组进行排序 [英] matlab - Sort cells array according mode

查看:143
本文介绍了Matlab-根据模式对单元格数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用方向变化的特定列对矩阵进行排序? 我已经尝试过了,但是不起作用.

How can I sort a matrix using a specific column, with changing direction? I've tried this, but don't work.

data:
    A1  5  P19
    A2  7  P45
    A3  8  P7

[Y,I] = sort(data(:,3), 'descend');
B = data(Y,3);

我需要获得:

In Ascending
    A3  8  P7
    A1  5  P19
    A2  7  P45

In descending:
        A2  7  P45
        A1  5  P19
        A3  8  P7

感谢您的帮助.

推荐答案

根据第3列以字母顺序(不是您想要的顺序)进行排序:

To sort in alphabetical order (not what you want) according to column 3:

[Y,I] = sort(data(:,3)); %// I gives the indices of sorted rows 
B_asc = data(I,:); %// ascending
B_des = data(I(end:-1:1),:); %// descending

根据第3列以数字的顺序(似乎是您想要的)排序,而不带"P":

To sort in numerical order (which seems to be what you want) according to column 3 without the "P":

aux = strvcat(data(:,3)); %// put column 3 into matrix form, with trailing spaces
[Y,I] = sort(str2num((aux(:,2:end)))); %// convert to number and sort
B_asc = data(I,:); %// ascending
B_des = data(I(end:-1:1),:); %// descending

这篇关于Matlab-根据模式对单元格数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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