views:

2548

answers:

2

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?

+5  A: 

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;

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'
gnovice
you can also use **cat(1,s.value)** to concatenate the values
Amro
Thank you so much for reply , I will try to use this function . Please can I get your email address in order to sent you my matlab codthanks
zenab
@zenab: My email address is on my profile. Feel free to drop me a line if you have anything you want to discuss directly.
gnovice
Hi Mr. gnovice I would like to extend my sincere thanks and appreciation to you for you helping ... I applied your solution (because I have a very large Structure) it work efficiently Zenab
zenab
+2  A: 

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