Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在matlab中基于一个字段对结构数组中的所有字段进行排序_Matlab_Struct - Fatal编程技术网

在matlab中基于一个字段对结构数组中的所有字段进行排序

在matlab中基于一个字段对结构数组中的所有字段进行排序,matlab,struct,Matlab,Struct,假设列f1和f2是一个名为inputStruct的结构,列f3和f4是另一个名为outputStruct的结构(很抱歉格式不正确)。我想按照inputStruct.f2的升序对inputStruct中的所有字段进行排序,以便输出为outputStruct(f3列和f4列)。我将如何处理这个问题 f1_f2 | f3_f4 a_uu4c_u1 b_uu2b_u2 c_uu1|e_u3 d_u5a_u4 e_uu3|d_u5使用sort获取outputStruct.f4和相应的索引。使用这些索引重新

假设列f1和f2是一个名为inputStruct的结构,列f3和f4是另一个名为outputStruct的结构(很抱歉格式不正确)。我想按照inputStruct.f2的升序对inputStruct中的所有字段进行排序,以便输出为outputStruct(f3列和f4列)。我将如何处理这个问题

f1_f2 | f3_f4

a_uu4c_u1

b_uu2b_u2

c_uu1|e_u3

d_u5a_u4


e_uu3|d_u5

使用
sort
获取
outputStruct.f4
和相应的索引。使用这些索引重新排列
inputStruct.f1
并获取
outputStruct.f3

[outputStruct.f4, ind] = sort(inputStruct.f2);
outputStruct.f3 = inputStruct.f1(ind);
或者,对于多个字段,只需循环遍历所有字段:

[~, ind] = sort(inputStruct.f2);   %Sorting according to field f2
fns = fieldnames(inputStruct);     %Retrieving the names of all the fields

for k = 1:numel(fns)               %Looping for each field
    outputStruct.(fns{k}) = inputStruct.(fns{k})(ind);
end
%Note: This creates outputStruct with the same fields as that of inputStruct
%but that can be adjusted if needed

具体输出是什么?编辑问题以包含输出。请修改格式。用MATLAB的语法编写会更好。请注意,字段不能以数字开头,因此添加的编辑无效。我理解字段不能是数字,这是我正在开发的代码的一个概念问题。那么,当你理解这一点时,为什么要做出错误的陈述?这样更好吗?“我想对所有字段进行排序”--你不能对结构的字段进行排序,它们没有顺序。这是一本字典。您可以对结构数组的元素进行排序,也可以对矩阵的行进行排序,等等。此外,请使用编辑框中可用的格式化命令。“{}”按钮将所选文本格式化为代码。很难理解,
a\uuu 4
是表示字段名,还是使用
\uuu
键作为间距?如果是这样,请使用适当的间距格式,这将有助于我们理解您。