Logical condition for a while lopp exits prematureley

Hi, I have this while loop criteria,
while((std(PenPar(:,2),0,1) > 50 && std(PenPar(:,3),0,1) > 50 && std(PenPar(:,4),0,1) > 50) || Stopper==500)
but it stops prematurely; only one of the and terms are satisfied.
Can someone explain me why it is so? Best reagrds

5 Comments

Whats is your stopper value? It changes or fixed?
Ah sorry for not supplying enough info, it is changing. All of them should be decreasing as time goes, but at unknown rates. Edit: Miss read, stopper never reaches his target when the loop ends, it never goes above 5 in the problem I'm currently working on.
I verbose the st.dev at each iteration so I can see that it ends when one of them goes bellow 50.
As stopper is less then 500. This statement counts to 0. If any std out of three is not satisfied then loop will surely stops. You have to check your std.
Ok, so my issue was that I don't want it to stop just because one of the std reaches bellow 50. Which I see, as I verbose the std constantly. Maybe if I specify my problem better. I want all statements to be true for it to stop. All three columns has to have a std bellow 50 for it to stop. Not just one of them. The last verbose is:
Running BigLoop itteration 1365 current st.dev =93.56746 124.6802 50.11569
so there is no way it is something with the std.

Sign in to comment.

 Accepted Answer

while all(std(PenPar(:,2:4),0,1) > 50) || Stopper == 500
does, what you ask for. But this is equivalent to your code, because && has a higher precedence than || . Therefore I'm still convinced, that your code works fine and the loop is not finished too early. Are you sure that the loop is not terminated by the Stopper limit?

4 Comments

Yes, I have identified the problem here. I also baffles me why it wont work. Stopper gets an addition or reset in the following way:
if PnewPenalty < fM
NewAddition=[PnewPenalty Pnew];
[~,RowIndex] = max(SumResiduals);
PenPar(RowIndex,:) = NewAddition;
disp('Replacing worst parameterset with:');
disp(' r² alpha beta k_i ');
dispParRes = [num2str(NewAddition(1,1)),' ',num2str(NewAddition(1,2:end))];
disp(dispParRes);
Stopper=1;
else
Stopper=Stopper+1;
end
And my last verbose before it finishes is as follows:
Running BigLoop itteration 1365 current st.dev =93.56746 124.6802 50.11569
Replacing worst parameter set with:
r² alpha beta k_i
0.0012705 620.0898 827.0099 283.9735
Edit: which is printed with
LoopMessage = ['Running BigLoop itteration ',num2str(ii), ' current st.dev =', num2str(std(PenPar(:,x+1:end)))];
disp(LoopMessage);
edit2: x=1 in this case
@Philip: I still do not see the problem. You can be sure, that all values are checked correctly in the condition of the WHILE command. The loop will only stop, if all standard deviations are smaller 50 or Stopper is 500. Promissed. If you "last verbose output" tells you anything else, the problem is in the code for showing this output. Note, that you use "std(PenPar(:,x+1:end))", so either "x+1:end" is wrong, or it matters that you calculate std along the first non-singelton dimension here, but specify the 1st dimension in the while-condition.
You can check this using the debugger, or:
ready = false;
while ~ready
...
ready = (all(std(PenPar(:,2:4),0,1) > 50) || Stopper == 500);
if ready
disp(std(PenPar(:,2:4),0,1))
disp(std(PenPar(:,x+1:end))) % Is this the same?
disp(Stopper)
end
end
You will see, that Matlab does not do any magic. If the results baffle you, the problem must be your expectations.
Haha, thanks for the help! Indeed the error is in the verbose command. How tedious to not notice. Yes I did not expect magic from MATLAB.
:-) Fine. I'm glad that your problem is solved. Have a nice day.

Sign in to comment.

More Answers (0)

Categories

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

Asked:

on 28 Oct 2016

Commented:

Jan
on 28 Oct 2016

Community Treasure Hunt

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

Start Hunting!