【问题标题】:Iterate over all possible combinations of input variables in mathematica迭代mathematica中输入变量的所有可能组合
【发布时间】:2017-06-27 16:00:13
【问题描述】:

我在 Mathematica 中有一个自定义函数,语法如下:

outputval = myfunc[r, sigma, S, K, T, lambda, eta1, eta2, p]

当函数以上面的数字输入值调用时,它会输出单个输出值。

对于每个输入变量,我有 5 个不同的值。我想在我的函数中输入 9 个输入变量的所有 5 个值的所有组合,并在第 10 列中导出一个包含 9 个输入值及其各自输出值的 CSV 文件。

我对 Mathematica 很陌生,我不知道该怎么做。任何帮助表示赞赏:)

【问题讨论】:

    标签: function loops iteration wolfram-mathematica combinations


    【解决方案1】:

    一个小例子将说明如何得到你想要的:

    xvals = {1, 2}
    yvals = {3, 4}
    {Sequence @@ #, f @@ #} & /@ Tuples[{xvals, yvals}]
    

    警告:5^9==1953125。因此,您可以使用Do 循环并直接写入文件而不是创建这些列表。举例说明:

    fmt = StringTemplate["``,``,``"];
    Do[Print[fmt[x, y, f[x, y]]], {x, xvals}, {y, yvals}]
    

    您需要将Print 替换为WriteLine

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-11
      • 2022-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-31
      • 1970-01-01
      相关资源
      最近更新 更多