MATLAB Answers

If condition with multiple OR statements

27 views (last 30 days)
drummer
drummer on 10 Jul 2020 at 15:15
Edited: Stephen Cobeldick on 10 Jul 2020 at 17:57
Hi,
I would like to understand what's the problem here.
PURPOSE: Not print 'd', if the multiple OR statements are satisfied.
clearvars d
for d = 1 : 26
if (d ~= 1 || d ~= 3 ||d ~= 7 ||d ~= 9 ||...
d ~= 18 || d ~= 20 || d ~= 24 || d ~= 26)
d
end
end
So that only cases that d = 2, 4, 5, 6, 8 are printed.
But it happens that all values are showed... why?
Wouldn't the code above be the negative of the code below? * This code below works, btw.
clearvars d
for d = 1 : 26
if (d == 1 || d == 3 ||d == 7 ||d == 9 ||...
d == 18 || d == 20 || d == 24 || d == 26)
else
d
end
end
Thanks

  1 Comment

Stephen Cobeldick
Stephen Cobeldick on 10 Jul 2020 at 17:56
"But it happens that all values are showed... why?"
Because that is the correct interpretation of the boolean logic that you specified. Can you show us one finite integer value d for which this statment returns false?:
d~=1 || d~=3
(hint: there is no such value, that statement will always return true because at most one of its terms can be false for any value of d. But to get a false output you would need all of its terms to be false, and that will never happen).
"Wouldn't the code above be the negative of the code below?"
No, that would be incorrect. One correct way to form the negation is to use De Morgan's laws:
and use AND instead of OR:
d~=1 && d~=3 && ...
But you should skip all of that and just use the standard MATLAB approach, i.e. ismember.

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 10 Jul 2020 at 15:32
Let's take a look at a shorter version of your code.
for d = 1 : 3
if (d ~= 1 || d ~= 3)
disp(d)
end
end
When d = 1, we evaluate your if condition. (d ~= 1) is false, (d ~= 3) is true, and false || true is true. So we display d.
When d = 2, we evaluate your if condition. (d ~= 1) is true, so MATLAB doesn't need to evaluate the rest of the expression. true || anything is true. So we display d.
When d = 3, we evaluate your if condition. (d ~= 1) is true, so MATLAB doesn't need to evaluate the rest of the expression. true || anything is true. So we display d.
I would use ismember in this situation.
for d = 1:3
if ~ismember(d, [1 3])
disp(d)
end
end
When d = 1, ismember(d, [1 3]) is true so ~ismember(d, [1 3]) is false and we don't display d.
When d = 2, ismember(d, [1 3]) is false so ~ismember(d, [1 3]) is true and we display d.
When d = 3, ismember(d, [1 3]) is true so ~ismember(d, [1 3]) is false and we don't display d.

  2 Comments

drummer
drummer on 10 Jul 2020 at 15:43
Thanks @Steven Lord.
So, for MATLAB, the 1st statement being true is enough to execute everything within the if condition.
Indeed, I have already solved my problem using the negative of ismember.
I just would like to understand why using the first scenario would show every 'd' value.
Now it's clear.
Cheers.
Steven Lord
Steven Lord on 10 Jul 2020 at 16:54
The || operator short-circuits (as does the && operator.)
x = true || error("This never gets thrown")
x = false && error("Nor does this")
true or anything is true. MATLAB doesn't need to know what happens when it runs "anything", so it doesn't even try to run it.
false and anything is false.
Note that if it didn't, there would be two reasons for the "anything" sections to throw an error in this particular case.
>> y = error("Can we call error with an output argument?")
Error using error
Too many output arguments.

Sign in to comment.

More Answers (0)