How to find the most minimum value of 100 minimum values?

Dear all,
I have a (100 iterations) while loop, during each iteration the program finds a minimum value,
so I'll have 100 minimum values.How to find the most minimum one of the 100 minimum values?

 Accepted Answer

Mohamed - if your while loop produces a minimum value in each iteration, then just save it to an array/vector. When the code exits the while loop, just find the minimum of that vector. For example
minValuesFound = [];
while someConditionIsTrue
% do some work
% find the minimum
% add the minimum value to the vector
minValuesFound = [minValuesFound ; newMinForThisIteration];
end
% find the overall min
overallMin = min(minValuesFound);
Try something like the above - the trick is to keep track of all minimum values from each iteration and then find the minimum afterwards. Alternatively, you could just keep track of the overall minimum value at each iteration
overallMin = Inf;
while someConditionIsTrue
% do some work
% find the minimum
% is this minimum smaller?
if newMinForThisIeration < overallMin
overallMin = newMinForThisIeration;
end
end
Either way should work for you!

4 Comments

Thanks Geoff Hayes, I tried both of the two methods, but I got an error "out of memory" as my program has many matrices.
Geoff's code only adds two 8 byte variables: overallMin and newMinForThisIeration, so it's not his code that is causing your out of memory situation. You must be saving all of the matrices. Don't do that.
Mohamed - you may need to provide some more detail as to how your program uses the many matrices and how you have integrated the above "minimum finding code" into it. You had said that there were only 100 iterations - is this not the case?
You are right. After a revision to my code , it works well for 100 and 1000 iterations. Thank you all.

Sign in to comment.

More Answers (1)

Another option is to use the cummin function.
It might do what you want.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!