If condition with multiple OR statements
351 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commento
Stephen23
il 10 Lug 2020
Modificato: Stephen23
il 10 Lug 2020
"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.
Risposta accettata
Steven Lord
il 10 Lug 2020
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 Commenti
Steven Lord
il 10 Lug 2020
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.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Whos in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!