This algorithm might be be more effective than that based on expand.grid
:
n <- 3
z <- rep(0,n)
answer <- t(apply(combn(0:n,2),2,function(k) {z[k]=1;z}))
# [,1] [,2] [,3]
# [1,] 1 0 0
# [2,] 0 1 0
# [3,] 0 0 1
# [4,] 1 1 0
# [5,] 1 0 1
# [6,] 0 1 1
[EDIT] I noticed that my original solution misses a trivial case of all zeros,
which can be easily fixed:
rbind(unname(z),answer)
# [,1] [,2] [,3] [,4]
# [1,] 0 0 0 0
# [2,] 1 0 0 0
# [3,] 0 1 0 0
# [4,] 0 0 1 0
# [5,] 0 0 0 1
# [6,] 1 1 0 0
# [7,] 1 0 1 0
# [8,] 1 0 0 1
# [9,] 0 1 1 0
# [10,] 0 1 0 1
# [11,] 0 0 1 1
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…