如何在MATLAB中排序结构数组?

时间:2020-12-31 16:00:31

I'm working with an image retrieval system using color histogram intersection in MATLAB. This method gives me the following data: a real number which represents the histogram intersection distance, and the image file name. Because they are different data types, I store them in structure array with two fields, and then I save this structure in a .mat file. Now I need to sort this structure according to the histogram intersection distance in descending order in order to retrieve the image with the highest histogram intersection distance. I've tried many methods to sort this data but without result. Please can you help me solve this problem?

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

2 个解决方案

#1


12  

Here's one example of how you could do this, using the function MAX instead of having to sort:

这里有一个例子说明你如何做到这一点,使用函数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;

EDIT : If you would like to get the N highest values, and not just the maximum, you can use SORT instead of MAX (as Shaka suggested). For example (using the above structure):

编辑:如果你想要得到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'

#2


15  

It's also possible to sort the entire structure.

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

To build off of gnovice's example...

以格鲁瓦的例子为基础……

% 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);

#1


12  

Here's one example of how you could do this, using the function MAX instead of having to sort:

这里有一个例子说明你如何做到这一点,使用函数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;

EDIT : If you would like to get the N highest values, and not just the maximum, you can use SORT instead of MAX (as Shaka suggested). For example (using the above structure):

编辑:如果你想要得到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'

#2


15  

It's also possible to sort the entire structure.

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

To build off of gnovice's example...

以格鲁瓦的例子为基础……

% 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);