问题描述
我是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:如何按特定的列名对矩阵进行排序,并让行名遵循顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!