MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   如何在MATLAB中对结构数组进行排序? (https://www.labfans.com/bbs/showthread.php?t=22798)

poster 2019-12-10 16:49

如何在MATLAB中对结构数组进行排序?
 
我正在使用在MATLAB中使用颜色直方图交集的图像检索系统。此方法提供了以下数据:代表直方图相交距离的实数和图像文件名。因为它们是不同的数据类型,所以我将它们存储在具有两个字段的结构数组中,然后将此结构保存在.mat文件中。现在,我需要根据直方图相交距离以降序对这种结构进行排序,以便检索具有最高直方图相交距离的图像。我尝试了许多方法来对这些数据进行排序,但是没有结果。请您能帮我解决这个问题吗?


回答:
以下是使用[URL="http://www.mathworks.com/help/techdoc/ref/max.html"]MAX[/URL]而不是进行排序的方法的一个示例:

%# 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; [B]编辑:[/B]如果您想获得N个最大值,而不仅仅是最大值,则可以使用[URL="http://www.mathworks.com/help/techdoc/ref/sort.html"]SORT[/URL]代替MAX( [URL="https://stackoverflow.com/questions/1497484/how-to-sort-structure-arrays-in-matlab/1498734#1498734"]如Shaka建议[/URL] )。例如(使用上面的结构):

>> N = 2; %# Get two highest values >> [values,index] = sort([s.value],'descend'); %# Sort all values, largest first >> topNFiles = {s(index(1:N)).file} %# Get N files with the largest values topNFiles = 'img2.jpg' 'img3.jpg'

[url=https://stackoverflow.com/questions/1497484]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 13:49

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.