たとえば|S|の場合 = 8、次の形式の行を含む256x8行列を取得するにはどうすればよいですか。
> sample(c(0,1),8,replace=T)
[1] 1 0 0 1 1 1 0 0
たとえば|S|の場合 = 8、次の形式の行を含む256x8行列を取得するにはどうすればよいですか。
> sample(c(0,1),8,replace=T)
[1] 1 0 0 1 1 1 0 0
多分これは役立ちます:
library(e1071)
bincombinations(8)
これは、はるかに高速な(そして間違いなくクリーンな)バージョンですbincombinations
:
fast.bincombinations <- function(p)
vapply(X = seq_len(p),
FUN = function(i)rep(rep(0:1, each = 2^(p-i)), times = 2^(i-1)),
FUN.VALUE = integer(2^(p)))
system.time(fast.bincombinations(24))
# user system elapsed
# 2.967 1.056 3.995
system.time(bincombinations(24))
# user system elapsed
# 11.144 12.111 53.687
bincombinations
数値の行列を出力することにも言及しましょう。これは悪い設計の私見です。
あなたはこれを行うことができます:
s = 8 # <-- |s| = 8
pset <- t(sapply(0:(2^s-1),intToBits))[,1:s] # <-- a matrix of 256x8 raws
pset <- apply(pset ,2,as.integer) # <-- raws to integers
結果:
> head(pset)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 0 0 0 0 0 0 0 0
[2,] 1 0 0 0 0 0 0 0
[3,] 0 1 0 0 0 0 0 0
[4,] 1 1 0 0 0 0 0 0
[5,] 0 0 1 0 0 0 0 0
[6,] 1 0 1 0 0 0 0 0
> tail(pset)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[251,] 0 1 0 1 1 1 1 1
[252,] 1 1 0 1 1 1 1 1
[253,] 0 0 1 1 1 1 1 1
[254,] 1 0 1 1 1 1 1 1
[255,] 0 1 1 1 1 1 1 1
[256,] 1 1 1 1 1 1 1 1
別の方法は次のとおりです。
s = 8;
res <- sapply(0:(s-1),function(x)rep(c(rep(0,2^x),rep(1,2^x)),2^(s-x-1)))