Array loop to find highest value

조회 수: 8 (최근 30일)
Highscore
Highscore 2020년 5월 17일
댓글: dpb 2020년 5월 17일
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
dpb
dpb 2020년 5월 17일
편집: dpb 2020년 5월 17일
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 2020년 5월 17일
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

댓글을 달려면 로그인하십시오.

채택된 답변

dpb
dpb 2020년 5월 17일
"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
Highscore
Highscore 2020년 5월 17일
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 2020년 5월 17일
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).

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by