我想在表上重复一个函数 n 次,对于 n=2 我有以下代码,由于我的 fc 每次都不同,我如何确定该函数已运行两次?
smat = Table[{9, 8, 10}, {3}]
f[x_?Table] := ReplacePart[
x, {{2, 1} -> x[[2]][[1]] - #, {2, 2} -> x[[2]][[2]] + #}] &@ fc[x[[2]][[1]]];
fc[k_?NumericQ] := Count[RandomReal[{0, 1}, k], x_ /; x < .1]
Nest[f, smat, 2]
最佳答案
这可能就是您想要的:
smat = Table[{9, 8, 10}, {3}]
ClearAll[f, fc];
f[x_List] :=
ReplacePart[
x, {{2, 1} -> x[[2]][[1]] - #, {2, 2} -> x[[2]][[2]] + #}] &@
fc[x[[2]][[1]]];
fc[k_?NumericQ] := Count[RandomReal[{0, 1}, k], x_ /; x < .1]
Nest[f, smat, 2]
ClearAll
清除这些符号以前的所有定义(以防万一)。 f[x_?Table]
不起作用;您需要 f[x_List]
,这意味着参数有一个 List
头(Table
不是 Head
,并且 ?
不是您想要的)。
虽然我不确定我是否真正回答了您的问题...
编辑:要明确的是,f[x_?something]
表示“将 something
应用于 x
,并且如果它返回 True
,计算后面 :=
的右侧。有关详细信息,请在 Mathematica 文档中查找 PatternTest
。
关于wolfram-mathematica - 在 Mathematica 中重复函数 n 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6590147/