Array loop to find highest value

8 views (last 30 days)
Highscore
Highscore on 17 May 2020
Commented: dpb on 17 May 2020
I have an array filled with random values between 0 and 1. These values are a result of an earlier image proces. I want to know which 5 images got the best result, but since the values are linked with the id in the array, I can't sort them descending because I could not find what image belongs to what value.
So I got the idea of creating the same array, but finding the highest value with a loop, the id is linked with it so I can call the name of the image and show the image. After that, I can overwrite this value with 0 to run the same loop again and get the new highest value (2nd highest overall) until I got the 5 highest values.
Unfortunately I have no clue on how to write this in matlab even tho I think this is a good approach. Any help by chance?
  2 Comments
dpb
dpb on 17 May 2020
Edited: dpb on 17 May 2020
Show us the actual data storage by which :the values are linked with the id in the array, :
A first ototh idea would be to save the results by plane instead of mixing them up together...or a place where cell array might be useful segregating device.
Highscore
Highscore on 17 May 2020
This is a double with (in this example) 1949 values ; all random mixed. Now if I call this;
[max_cos, id_max_cos] = max(cos_mean)
I get the max value and the id so I can show the image later and find out what image gave the best value.
Now I want the 5 best values, but I'm afraid if I sort them with the sort function the id's wont match anymore and it'll display the wrong images if I work with these, since the 5 highest values will be in spot '1', '2', .. '5'.
Since I'm nowhere to being a coder I could use any help I can find

Sign in to comment.

Accepted Answer

dpb
dpb on 17 May 2020
"Now I want the 5 best values, but I'm afraid if I sort them with the sort function the id's wont match anymore ..."
If that's all you're worried about, life is sweet! :)
nMax=5; % say, use variable to be able to change
[maxN_cos,ixMax]=maxk(cos_mean,nMax); % save the N maxima, locations to correspond
Now you can relate whatever with the locations returned.
Of course, you can do the same thing with the regular sort function by simply saving the first nMax elements of the two arrays; maxk just saves you an intermediate step. You also don't reorder the original array this way; of course you can recreate it by the index vector if that were to be needed. But, may as well use the simpler tool/path if available.
I thought your description implied some other variable or link to the image to try to find, not just the position in the array.
  2 Comments
Highscore
Highscore on 17 May 2020
And how would I call to this functions then? Right now I got this path;
Files=dir('C:\Users\admin\Desktop\Master\*');
and I use this to get the picture:
Files(id_max_cos).name
so I would need to change something to call the 5 (N) pictures.
Thanks for the help :)
dpb
dpb on 17 May 2020
Presuming the array size/order you're worried about preserving is 1:numel(Files) and that's the way you stored the values in the cos_mean array, then you just use whichever index you want into the sort order index array.
To iterate over all nMax it is simply
for i=1:nMax
ffname=fullfile(Files(ixMax(i).folder,Files(ixMax(i).name); % build fully-qualified name
m=imread(ffname); % and read the file (or whatever)
end
It's just one level of redirection to the location specified by the index array into the big array...you do have to keep the various arrays in synch; you might consider making stuff into a struct array with fields for the pieces instead of independent arrays (much like the dir function above with the folder, file name, etc., for each file as one entity).

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!