开发者

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!


  1. Notice that you have If statements for both b > m and b < m, as well as b > k and b < k. These pairs can each be two halves of the one If statement.
  2. Convert all the top level conditionals into a Which statement. Look up the documentation in Mathematica for more information.
  3. You can probably also convert the relationships between b and l to simpler Which statements.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜