MATLAB:如何按特定的列名对矩阵进行排序,并让行名遵循顺序? [英] MATLAB: how to sort a matrix by a specific column name and also let the row names follow the order?

查看:608
本文介绍了MATLAB:如何按特定的列名对矩阵进行排序,并让行名遵循顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MATLAB的新手.我有一个名为da的数据结构.我想对da.mat的第一列进行排序,并希望让da.rid和其他列遵循重新排列的顺序. da.cid包含列名,da.rid包含行ID.

I am new to MATLAB. I have a data structure named da. I want to sort the first column of da.mat and want to let da.rid and the other columns to follow the rearranged order. da.cid contains the column names and da.rid contains the row IDs.

da = 
    mat: [22268x377 single]
    rid: {22268x1 cell}
    rhd: {''}
  rdesc: {22268x1 cell}
    cid: {377x1 cell}
    chd: {0x1 cell}
  cdesc: {377x0 cell}

此外,如果我想使用其他列而不是da.mat的第一列,而我将从da.cid中获得第一列,我该如何实现呢? 例如,如果我想在cid中查找列名'A02'并使用它来选择da.mat的特定列进行排序.请你帮助我好吗?谢谢.

Also, if I want to use some other column instead of the first column of da.mat and which I will get from da.cid, how can I acheive it? For example, if I want to look for the column name 'A02' in cid and use it to select the specific column of da.mat for sorting. Could you please help me? Thanks.

伍迪

推荐答案

假设其他列是指da.mat本身的其他列,则可以尝试-

Assuming by other columns, you mean other columns of da.mat itself, you may try this -

[val,ind] = sort(da.mat(:,1))
da.mat = da.mat(ind,:)
da.rid = da.rid(ind)

如果要使用其他列号而不是1进行排序,并且要基于字段cid中的名称,请使用此-

If you are looking to use some other column number instead of 1 for sorting and based on the names in the field cid, use this -

cid_matchcol =  'A02'; %// column name to be used from `da.cid` to choose column of `da.mat`

base_col = find(strcmp(da.cid,cid_matchcol),1)
[val,ind] = sort(da.mat(:,base_col))
da.mat = da.mat(ind,:)
da.rid = da.rid(ind)

这篇关于MATLAB:如何按特定的列名对矩阵进行排序,并让行名遵循顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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