用于循环输出到mathematica列表

时间:2013-05-22 21:07:55

标签: list loops for-loop wolfram-mathematica output

我似乎无法将这个混乱的输出放到列表中,我知道代码很差,但是它有效。它从循环的每次迭代中获取一个值,我需要这个输出作为列表。我可以手动完成,但它有1000多个值的痛苦...提前感谢您的帮助。

For[r = 1, r <= 5, r++, 

 draws = 1000;
 FredList = 
  Reap[For[i = 1, i <= draws, i++, 
     Sow[RandomSample[Join[Table["a", {16}], Table["b", {16}]], 
       2]]];][[2, 1]];
 tally = Tally[FredList];
 Sort[tally][[All, 2]];
 rules = Rule @@@ tally;
 {{"a", "a"}, {"b", "b"}, {"b", "a"}, {"a", "b"}} /. rules;
  Plotpoints = {{"a", "a"}, {"b", "b"}, {"b", "a"}, {"a", "b"}} /. 
  rules; Print[Plotpoints[[1]]]]

2 个答案:

答案 0 :(得分:1)

也可以使用Table作为外部循环。

With[{draws = 1000, maxr = 5, 
  pop = Join @@ (ConstantArray[#, 16] & /@ {"a", "b"})}, 
 Table[
  Count[Table[RandomSample[pop, 2], {draws}],
   {"a", "a"}], 
  {maxr}]
 ]

答案 1 :(得分:1)

您可以使用Sow and Reap(或其他列表工具,如AppendTo)代替打印:

Reap[For[r = 1, r <= 5, r++, draws = 1000;
    FredList = Reap[For[i = 1, i <= draws, i++, 
    Sow[RandomSample[Join[Table["a", {16}], Table["b", {16}]],2]]];][[2, 1]];
    tally = Tally[FredList];
    (*Honestly, I don't understand the effect of this Sort here*)Sort[tally][[All, 2]];
    rules = Rule @@@ tally;
    {{"a", "a"}, {"b", "b"}, {"b", "a"}, {"a", "b"}} /. rules;
    Plotpoints = {{"a", "a"}, {"b", "b"}, {"b", "a"}, {"a", "b"}} /.rules;
    Sow[Plotpoints[[1]]]
]][[2, 1]]