Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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_Sorting_Struct - Fatal编程技术网

Matlab中结构的排序

Matlab中结构的排序,matlab,sorting,struct,Matlab,Sorting,Struct,在Matlab中,我有一个结构数组(“在此上排序”,…) 如何在此上对数组进行排序 我正在尝试做的例子 list_to_sort = [struct('a', 4), struct('a', 0), struct('a', 3), struct('a', 1)] sort(list_to_sort on 'a') = [struct('a', 0), struct('a', 1), struct('a', 3), struct('a', 4)] 编辑:问题不是,因为另一个问题在

在Matlab中,我有一个
结构数组(“在此上排序”,…)

如何在此上对数组进行排序

我正在尝试做的例子

list_to_sort = [struct('a', 4), struct('a', 0), struct('a', 3), struct('a', 1)]
sort(list_to_sort on 'a') = 
      [struct('a', 0), struct('a', 1), struct('a', 3), struct('a', 4)] 

编辑:问题不是,因为另一个问题在结构中有需要排序的数组,而这是一个需要排序的结构数组。

正如您可能已经了解到的,正常的
排序在结构上不起作用。然而,您可以构建一个值数组,然后对其进行排序,并使用新的排序对原始结构数组进行重新排序

从我们的结构数组开始:

list_to_sort = [struct('a', 4), struct('a', 0), struct('a', 3), struct('a', 1)]
将结构字段值获取到数组中:

a_values = [list_to_sort.a]

a_values =
   4   0   3   1
现在,对a_值进行排序,保留第二个返回值,从中我们可以得到排序值的原始索引

[~,inds] = sort(a_values)
inds =    
   2   4   3   1
最后,使用这些索引对结构进行重新排序:

sorted_list = list_to_sort(inds)

>> disp([sorted_list.a])
   0   1   3   4

正如您可能已经了解到的,正常的
排序
在结构上不起作用。然而,您可以构建一个值数组,然后对其进行排序,并使用新的排序对原始结构数组进行重新排序

从我们的结构数组开始:

list_to_sort = [struct('a', 4), struct('a', 0), struct('a', 3), struct('a', 1)]
将结构字段值获取到数组中:

a_values = [list_to_sort.a]

a_values =
   4   0   3   1
现在,对a_值进行排序,保留第二个返回值,从中我们可以得到排序值的原始索引

[~,inds] = sort(a_values)
inds =    
   2   4   3   1
最后,使用这些索引对结构进行重新排序:

sorted_list = list_to_sort(inds)

>> disp([sorted_list.a])
   0   1   3   4

关于什么排序?目录名称Size?@adrian我想根据变量sort\u对这是一个number@rayryeng我尝试使用您将其标记为的解决方案,但它在结构中有列表,而不是结构列表。所以这个问题是不同的。@rayryeng我道歉,今天早上我只是很匆忙,下次我将在提供足够的细节之前不发布这个问题。请将其取消标记为副本,好吗?结构中没有数组,您无法理解结构构造函数<代码>列表到排序=[struct('a',4),struct('a',0),struct('a',3),struct('a',1)]
列表到排序=struct('a',{4,0,3,1})
都产生相同的结果。排序与什么有关?目录名称Size?@adrian我想根据变量sort\u对这是一个number@rayryeng我尝试使用您将其标记为的解决方案,但它在结构中有列表,而不是结构列表。所以这个问题是不同的。@rayryeng我道歉,今天早上我只是很匆忙,下次我将在提供足够的细节之前不发布这个问题。请将其取消标记为副本,好吗?结构中没有数组,您无法理解结构构造函数<代码>列表排序=[struct('a',4),struct('a',0),struct('a',3),struct('a',1)]和
列表排序=struct('a',{4,0,3,1})
都产生相同的结果。