nested if conditionalConstruct in Mathematica
Do you know If and how can I rewrite the code below in a better form for different values of m,k,l?
It is a nested if loop that I want to to check b for all m,k,l, but the code below is too big, I would like to simplify it. Can I?
If[b > m, If[(b - 1) > k, If[(b - 2) > l, b - 3, b - 2],
If[(b - 1) > l, b - 2, b - 1]], If[b < m,
If[(b + 1) > k, If[(b + 2) < l, b + 3, b + 2], If[(b + 1) < l, b + 2, b + 1]],
If[b > k, If[(b - 1) > l, b - 2, b - 1], If[b < k, If[(b + 1) < l, b + 2, b + 1],
If[b > l, b - 1, If[b < l, b + 1, b]]]]开发者_如何学C]]
Thanks!
- Notice that you have
If
statements for bothb > m
andb < m
, as well asb > k
andb < k
. These pairs can each be two halves of the oneIf
statement. - Convert all the top level conditionals into a
Which
statement. Look up the documentation in Mathematica for more information. - You can probably also convert the relationships between
b
andl
to simplerWhich
statements.
精彩评论