On multiple logical test failing in if statement

Hello, I am trying to run an if statement with multiple conditions as below,
if abs(a-a_t)>0.0001 && abs(b-b_t)>0.00001 && abs(c-c_t)>0.0001 && abs(d-d_t)>0.00001
x=0;
y=0;
end
Even when all the conditions are satisfied, the values of x and y are not updated. But, it works if we have just two conditions (I've tried different combinations of two variable values which work, but using all conditions in separate brackets doesn't work). MATLAB documentation says that multiple logical operators could be used, but it doesn't seem to be working in my case. Is there a limit to the number of logical operators that could be combined? Or, am I missing something?
Thanks for your advice.

2 Comments

"Is there a limit to the number of logical operators that could be combined?"
No.
"Or, am I missing something?"
Probably. How are you checking the output? Please show us the exact input values (all eight of them). Please upload your code by clicking the paperclip button.
Hello Stephen,
Thanks for your quick reply.
This is puzzling, not to mention embarrassing too. I tried to run the code again and found that it is running. I am unsure what was the problem last time (it is the exact same code and inputs as back then). Thanks again for your reply, though!

Sign in to comment.

 Accepted Answer

dpb
dpb on 8 Jul 2018
Edited: dpb on 8 Jul 2018
Apparently the issue was likely one of the cases wasn't actually T or some similar misstep, but illustrates the difficulties inherent in writing code with sequentially-named variables. Use arrays instead.
If had defined a variable data and commensurate test value, then the following could be written much more succinctly and much easier to both code and debug--
data=[a b c ...]; % the data array (*)
test=[a_t b_t c_t ...]; % associated test values
lims=[0.0001 0.00001, 0.0001, ...]; % and criteria
if all(abs(data-test))<lims % see if all ok
....
(*) And, of course, this is not intended to suggest one should write the specific line concatenating all these variables to create the one; instead use the vector features of Matlab when creating the variables initially, either by reading one or more files or the result of other calculations, whatever.
Don't create the problem in the beginning to have to try to fix later...

7 Comments

This is an elegant way to deal with the scenario. Thanks for your suggestion.
To explain a little bit further, I have a while loop to check and run for the convergence of the variables a, b, c, d, each of which has it's own set of calculations and convergence rates. The only way (I know of, that is) I could put them into an array is by concatenating.
I have those if statements, as, the values inside the if statement are also a set of outputs and would be retained once the values have converged.
There's undoubtedly a way to incorporate all of the above in a "more Matlab'y" syntax to if not totally do away with at least minimize the use of separate variables for what are fundamentally similar uses.
Attach a sample of the code for others to look at and probably will get several suggestions on factoring it...
I unfortunately cannot share the code, but, I could give something like a pseudo-code. It's as follows,
I have four variables a, b, c and d which are calculated by iterations. These depend on three other variables x,y and z, which again are solved by iterations.
while abs(a-a_temp)>0.0001 && abs(b-b_temp)>0.0001 && abs(c-c_temp)>0.0001 && abs(d-d_temp)>0.0001
a= a_temp;
b=b_temp;
c=c_temp;
d=d_temp;
while (x-x_temp)>0.0001
calculations for x
.....
x_temp=....
end
......
while (y-y_temp)>0.0001
calculations for y
.....
y_temp=....
end
......
while (z-z_temp)>0.0001
calculations for z
.....
z_temp=...
end
....
....
a_temp=...
b_temp=...
c_temp=...
d_temp=...
if abs(a-a_temp)>0.0001 && abs(b-b_temp)>0.00001 && abs(c-c_temp)>0.0001 && abs(d-d_temp)>0.00001
x=0; %%to reset the values so while loop for x can work
y=0;
z=0;
end
Please let me know if this would suffice. Thanks again for your help.
Look ok to me. However x, y, and z only get reset to 0 if ALL 4 conditions hold. Are you sure that's what you want, and not to reset if ANY of the conditions is met?
Set a breakpoint on that "if" line to learn why it never goes in there.
Well x, x_temp, y, y_temp, z, and z_temp each do what they do independent of each other (i.e. x and x_temp don't depend on y and z, or a, b, c, or d). So each loop for x, y, and z is independent and the order doesn't matter as far as I can see. There is no break or continue so each must get done in turn.
Likewise, the big if statement doesn't depend on x, x_temp, y, y_temp, z, or z_temp so it's independent and will get tested no matter what.
I don't know what "most sensitive" means (maybe first to converge?) but if you're talking about short circuiting and not testing b, c, and d if abs(a-a_temp)>0.0001, then that's not true. It will test for b, c, and d because you have && in there not ||. Anyway, even if it was || and you did short circuit and not test the others, you'd only save like a half a nanosecond or something - not enough to worry about.
Thank you for pointing this out. It should be an "or" condition. I am also adding breakpoints to see if there's something else I'm missing.
Sorry, I deleted the previous comment instead of editing it (Gist- I used && because by the time the first value converges, remaining three too should have converged, so, when abs(a-a_t)<0.0001, everything else too should be lesser than 0.0001).
Yes, I meant that the first value would converge finally. Thus, if the first condition were to be satisfied, all the remainder should be satisfied by nature of the problem. But, still, I think that using "or" condition would be the better option, in case something unexpected comes up.
Also, I thought that if the first condition were not met, then && would skip the remainder, as the logical statement would anyway result in a 0, whereas the "or" would go on to check the remainder as we could have a result of 1. Would this be the other way around?

Sign in to comment.

More Answers (0)

Products

Release

R2017b

Asked:

on 8 Jul 2018

Edited:

on 9 Jul 2018

Community Treasure Hunt

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

Start Hunting!