【发布时间】:2013-06-20 04:27:58
【问题描述】:
我正在尝试按指定参数按降序对嵌套结构的顺序进行排序。请参考以下嵌套结构:
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;
我的目标是将上面的结构按最高的 classAve 排序到最低。我想按父结构“struct”排序。作为我希望输出的说明,请参阅下面的代码。请注意,嵌套结构现在按 classAve 降序排列,但在父结构中重新分配。
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;
如果有人对完成此操作的简单方法有任何建议,我们将不胜感激。谢谢!
【问题讨论】:
-
struct的每个元素中的所有otherStruct是否具有相同的calssAve值? -
我首先建议不要对这样一个简单的数据进行这样的数据组织。
-
Shai,是的,struct 的每个元素中的所有 otherStruct 总是具有相同的 classAve 值。
-
Oleg,我正在处理的数据实际上非常大/复杂。如果我不使用嵌套结构,那么数据的组织将过于混乱。此外,我以最简单的形式展示了代码,这样我的问题就不会令人困惑。
标签: matlab sorting nested structure