1

我似乎无法将这个混乱的输出放入列表中,我知道代码很差,但它可以工作。它从循环的每次迭代中获取一个值,我需要这个输出作为列表。我可以手动完成,但是有 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]]]]
4

2 回答 2

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]]
于 2013-05-23T05:57:45.060 回答
1

也可以Table用于外循环。

With[{draws = 1000, maxr = 5, 
  pop = Join @@ (ConstantArray[#, 16] & /@ {"a", "b"})}, 
 Table[
  Count[Table[RandomSample[pop, 2], {draws}],
   {"a", "a"}], 
  {maxr}]
 ]
于 2013-05-23T05:47:53.637 回答