Created
May 31, 2012 13:21
-
-
Save nanxstats/2843391 to your computer and use it in GitHub Desktop.
Connectivity Simulation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sim <- function(i, neighbor = 3) { | |
relate = t(replicate(41, sample(1:41, neighbor))) # 随机生成各节点的邻居 | |
mzqidx = setdiff(16:41, setdiff(unique(as.vector(relate[1:15, ])), 1:15)) # 正向没中枪行的下标 | |
if(length(mzqidx) > 0) { # 仅处理存在没中枪行的情况 | |
mzq = relate[mzqidx, ] # 取出正向没中枪的行组成矩阵 | |
if(is.vector(mzq) == TRUE) { # 处理只有单行没中枪的情况 同样做成矩阵 | |
mzq = t(as.matrix(relate[mzqidx, ])) | |
} | |
zqx = sapply(apply(mzq, 1, intersect, 1:15), length) # 统计反向中枪情况 | |
if (any(zqx == 0)) { | |
return(1) | |
} | |
else { | |
return(0) | |
} | |
} | |
} | |
result = sapply(1:1e+5, sim, 2) | |
summary(as.factor(unlist(result))) | |
# 0 1 | |
# 346 99654 | |
result = sapply(1:1e+5, sim) | |
summary(as.factor(result)) | |
# 0 1 | |
# 11331 88669 | |
result = sapply(1:1e+5, sim, 4) | |
summary(as.factor(unlist(result))) | |
# 0 1 | |
# 43101 56844 | |
result = sapply(1:1e+5, sim, 5) | |
summary(as.factor(unlist(result))) | |
# 0 1 | |
# 71506 27442 | |
result = sapply(1:1e+5, sim, 6) | |
summary(as.factor(unlist(result))) | |
# 0 1 | |
# 83197 10649 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment