Arrays 如何在MATLAB中对结构数组进行排序?
我正在使用MATLAB中的颜色直方图相交的图像检索系统。该方法为我提供了以下数据:表示直方图相交距离的实数,以及图像文件名。因为它们是不同的数据类型,所以我将它们存储在带有两个字段的结构数组中,然后将此结构保存在.mat文件中。现在我需要根据直方图相交距离按降序对该结构进行排序,以便检索直方图相交距离最高的图像。我尝试了很多方法对这些数据进行排序,但都没有结果。您能帮我解决这个问题吗?下面是一个示例,说明如何使用该函数而不必排序:Arrays 如何在MATLAB中对结构数组进行排序?,arrays,matlab,sorting,matlab-struct,Arrays,Matlab,Sorting,Matlab Struct,我正在使用MATLAB中的颜色直方图相交的图像检索系统。该方法为我提供了以下数据:表示直方图相交距离的实数,以及图像文件名。因为它们是不同的数据类型,所以我将它们存储在带有两个字段的结构数组中,然后将此结构保存在.mat文件中。现在我需要根据直方图相交距离按降序对该结构进行排序,以便检索直方图相交距离最高的图像。我尝试了很多方法对这些数据进行排序,但都没有结果。您能帮我解决这个问题吗?下面是一个示例,说明如何使用该函数而不必排序: %# First, create a sample struct
%# First, create a sample structure array:
s = struct('value',{1 7 4},'file',{'img1.jpg' 'img2.jpg' 'img3.jpg'});
%# Next concatenate the "value" fields and find the index of the maximum value:
[maxValue,index] = max([s.value]);
%# Finally, get the file corresponding to the maximum value:
maxFile = s(index).file;
编辑:如果希望获得N个最大值,而不仅仅是最大值,可以使用而不是MAX()。例如(使用上述结构):
也可以对整个结构进行排序 以gnovice的例子为基础
% Create a structure array
s = struct('value',{1 7 4},'file',{'img1.jpg' 'img2.jpg' 'img3.jpg'});
% Sort the structure according to values in descending order
% We are only interested in the second output from the sort command
[blah, order] = sort([s(:).value],'descend');
% Save the sorted output
sortedStruct = s(order);
您也可以使用cat(1,s.value)连接值谢谢您的回复,我将尝试使用此函数。请告诉我您的电子邮件地址以便向您发送我的邮件thanks@zenab:我的电子邮件地址在我的个人资料中。如果您有任何需要直接讨论的问题,请随时给我写信。你好,格诺维奇先生,我想对您的帮助表示衷心的感谢和感谢。。。我应用了您的解决方案(因为我有一个非常大的结构),它工作效率很高。注意:要按文件名(或任何字符串)排序,您可以执行
[~,order]=sort({s.file})代码>,然后sortedStruct=s(订单)代码>。在这种情况下,您不能使用'down'
,除非这在未来版本的Matlab中实现。
% Create a structure array
s = struct('value',{1 7 4},'file',{'img1.jpg' 'img2.jpg' 'img3.jpg'});
% Sort the structure according to values in descending order
% We are only interested in the second output from the sort command
[blah, order] = sort([s(:).value],'descend');
% Save the sorted output
sortedStruct = s(order);