如何在MATLAB中对结构数组进行排序? [英] How to sort structure arrays in MATLAB?

查看:953
本文介绍了如何在MATLAB中对结构数组进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

以下是使用功能

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

%# 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个最大值,而不仅仅是最大值,则可以使用如Shaka所建议的).例如(使用上面的结构):

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 = 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'

这篇关于如何在MATLAB中对结构数组进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆