ライフゲーム

統計とは関係ないが,JavaScriptで書いたライフゲームをRに移植してみた。9行で書けた。

a = b = matrix(0, nrow=50, ncol=50)
a[24:26,25] = a[25,24] = a[26,26] = 1
for (g in 1:300) {
    b[2:49,2:49] = a[1:48,1:48] + a[1:48,2:49] + a[1:48,3:50] +
                   a[2:49,1:48]                + a[2:49,3:50] +
                   a[3:50,1:48] + a[3:50,2:49] + a[3:50,3:50]
    a = ifelse(b != 2, b == 3, a)
    heatmap(a, Rowv=NA, Colv=NA, scale="none", col=0:1, main=g)
}