推 bewilderment:感謝老師。解讀中..... 07/19 19:49
※ 引述《bewilderment (發燒夏天)》之銘言:
: 各位版友好,最近在一本書上看到了如下的code:
: datasample = Table[Random[Integer, {1, 10}], {1000}];
: frequence[data_List, n_] :=
: Apply[Plus, data /. n -> "a" /. x_Integer -> 0 /. "a" -> 1];
: Table[frequence[datasample, n], {n, 1, 10}]
: 我看不太懂frequence這個函式在做什麼,
先把指定的整數轉為字串"a",其他的整數轉為0,之後再把"a"轉成1;最後加總。
In[1]:= datasample = Table[Random[Integer, {1, 10}], {10}]
Out[1]= {6, 10, 9, 7, 3, 7, 2, 7, 5, 5}
In[2]:= frequence[data_List, n_] :=
Apply[Plus, data /. n -> "a" /. x_Integer -> 0 /. "a" -> 1];
In[3]:= frequence[datasample, 5]
Out[3]= 2
In[4]:= Count[datasample, 5]
Out[4]= 2
In[5]:= Table[Count[datasample, n], {n, 1, 10}]
Out[5]= {0, 1, 1, 0, 2, 1, 3, 0, 1, 1}
Count直接就可以達成。
--
養花種魚數月亮賞星星
http://chungyuandye.twbbs.org
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.233.129.177