Is it possible to simplify this branching statement??

2 views (last 30 days)
Is it possible to simplify this branching statement??

Accepted Answer

Walter Roberson
Walter Roberson on 17 Apr 2014
Yes. Use DeMorgan's law. ~(A OR B) is the same as ((~A) AND (~B)). Also use transitive properties.
So
~(x > 0.2 || y < 0.1)
is
x <= 0.2 && y >= 0.1
that && z == 10 gives
x <= 0.2 && y >= 0.1 && z == 10
on the other half,
~(x > 0.2 || z ~= 10)
is
x <= 0.2 && z == 10
and y >= 0.1 && that gives
y >= 0.1 && x <= 0.2 && z == 10
AND is transitive (provided that none of x, y, z are function calls instead of variables), so you can sort that in the same order as the first, into x y z order:
x <= 0.2 && y >= 0.1 && z == 10
and we are asked to == that against the first part which was
x <= 0.2 && y >= 0.1 && z == 10
and we can see that it is always true (unless, that is, x y or z is a function call, or one of them is NaN).
Ignoring the NaN case for the moment, this simplifies to not doing the "if" at all and just always doing the awesomeFunction() call.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!