在Matlab中,如何对嵌套结构的顺序进行排序? [英] In Matlab, how can I sort the order of a nested structure?

查看:258
本文介绍了在Matlab中,如何对嵌套结构的顺序进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过指定参数按降序对嵌套结构的顺序进行排序.请参考以下嵌套结构:

I am trying to sort the order of a nested structure in descending order by a specified parameter. Please refer to the following nested structure:

struct(1).otherStruct(1).name = 'A';
struct(1).otherStruct(1).classAve = 21;
struct(1).otherStruct(2).name = 'B';
struct(1).otherStruct(2).classAve = 21;
struct(1).otherStruct(3).name = 'C';
struct(1).otherStruct(3).classAve = 21;

struct(2).otherStruct(1).name = 'D';
struct(2).otherStruct(1).classAve = 13;
struct(2).otherStruct(2).name = 'E';
struct(2).otherStruct(2).classAve = 13;
struct(2).otherStruct(3).name = 'F';
struct(2).otherStruct(3).classAve = 13;

struct(3).otherStruct(1).name = 'G';
struct(3).otherStruct(1).classAve = 24;
struct(3).otherStruct(2).name = 'H';
struct(3).otherStruct(2).classAve = 24;
struct(3).otherStruct(3).name = 'I';
struct(3).otherStruct(3).classAve = 24;

我的目标是按最高级别从最低到最高对上述结构进行排序.我想按父结构"struct"进行排序.为了说明我想要的输出,请参考下面的代码.请注意,嵌套结构现在按classAve降序排列,但在父结构中重新分配.

My goal is to sort the structure above by the highest classAve to the lowest. I would like to sort by the parent structure "struct". As an illustration of what I would like the output to be, please refer to the code below. Notice that the nested structure is now in descending order by classAve but reassigned within the parent structure.

struct(1).otherStruct(1).name = 'G';
struct(1).otherStruct(1).classAve = 24;
struct(1).otherStruct(2).name = 'H';
struct(1).otherStruct(2).classAve = 24;
struct(1).otherStruct(3).name = 'I';
struct(1).otherStruct(3).classAve = 24;

struct(2).otherStruct(1).name = 'A';
struct(2).otherStruct(1).classAve = 21;
struct(2).otherStruct(2).name = 'B';
struct(2).otherStruct(2).classAve = 21;
struct(2).otherStruct(3).name = 'C';
struct(2).otherStruct(3).classAve = 21;

struct(3).otherStruct(1).name = 'D';
struct(3).otherStruct(1).classAve = 13;
struct(3).otherStruct(2).name = 'E';
struct(3).otherStruct(2).classAve = 13;
struct(3).otherStruct(3).name = 'F';
struct(3).otherStruct(3).classAve = 13;

如果有人对实现此目标的简便方法提出建议,将非常感谢您的帮助.谢谢!

If anyone has suggestions on an easy way to accomplish this, any help would be greatly appreciated. Thank you!

推荐答案

首先,我建议使用另一个变量名(例如structA)代替struct,因为这是一个

Firstly I'd suggest using another variable name (eg structA) instead of struct since that's a function to create structs.

然后解决您的问题(假设每个otherStruct子都有相同的classAve):

Then to solve your problem (assuming each otherStruct child has the same classAve):

classAve = arrayfun(@(ii) structA(ii).otherStruct(1).classAve,1:numel(structA));
[~, sort_idx] = sort(classAve,'descend');
structAsorted = structA(sort_idx);

第一行是最大的跳跃障碍;它提取大结构的每个数组元素中第一个otherStruct的索引.以下两行对内容进行排序很简单.

The first line is the largest hurdle to jump; it extracts the indices of the first otherStruct in each array element of the big struct. The following two lines are trivial for sorting stuff.

这篇关于在Matlab中,如何对嵌套结构的顺序进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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