1

在 Mathematica 中,我使用以下代码:

If[ x [[1]] <= 4 && x [[2]] <= 4, "True","False"]
True

这是我感到困惑的地方:

If[ True, count=count + 1, count=count]

我不知道如何从代码之前的行中获取答案

4

3 回答 3

4

为什么不只是

If[ x [[1]] <= 4 && x [[2]] <= 4, count++]

?

于 2012-09-18T15:19:49.573 回答
3

除了贝利撒留的回答之外,您还可以考虑:

count += Boole[x[[1]] <= 4 && x[[2]] <= 4]

另外,您说:“我不知道如何从代码之前的行中获取答案。”

你可以使用%

If[ x [[1]] <= 4 && x [[2]] <= 4, True, False];

If[%, count=count + 1, count=count]
于 2012-09-18T22:48:01.097 回答
0

Literally you could just fit one piece of code inside the other:

If[ 
   If[ x [[1]] <= 4 && x [[2]] <= 4, True, False],
   count=count + 1, count=count
]

Note that the strings "True" and "False" have been replaced with their logical values True and False

于 2012-09-18T15:27:16.707 回答