10

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

4

2 回答 2

15

也可以对整个结构进行排序。

以 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);
于 2009-09-30T15:08:50.907 回答
12

这是一个使用函数MAX而不必排序的示例,说明如何做到这一点:

%# 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 个最高值,而不仅仅是最大值,您可以使用SORT而不是 MAX(如 Shaka 建议的那样)。例如(使用上述结构):

>> 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'
于 2009-09-30T13:58:47.007 回答