假设我有一个P
如下所示的任意概率矩阵,
P = matrix(c(0.3,0.2,0.2,0.2,0.3,0.2,0.2,0.2,0.3),3,3)
P
[,1] [,2] [,3]
[1,] 0.3 0.2 0.2
[2,] 0.2 0.3 0.2
[3,] 0.2 0.2 0.3
对于单个邻接矩阵,它的生成方式类似于(未加权,无自放样)
tem = matrix(runif(3^2), nrow = 3)
tmpG = 1 * (tmpmat < P)
tmpG[lower.tri(tmpG)] <- 0
tmpG <- t(tmpG) + tmpG - diag(diag(tmpG))
但是,如果我需要生成100个邻接矩阵怎么办,所以我写下以下代码
G = list()
for (i in 1:rep) {
tmpmat = matrix(runif(n^2), nrow = n)
tmpG = 1 * (tmpmat < P)
tmpG[lower.tri(tmpG)] <- 0
tmpG <- t(tmpG) + tmpG - diag(diag(tmpG))
if (noloop) {
diag(tmpG) = 0
}
G[[i]] = tmpG
}
就我而言,n >10000
andT = 1000
非常慢,有什么更好的办法来改进它吗?