MATLAB: Array loop to find highest value

arrayloopMATLAB

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?

Best Answer

"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.