【发布时间】:2020-11-22 11:59:18
【问题描述】:
我有两个矩阵(1 和 2)相乘,得到一致性矩阵 (c)。
我需要将这些矩阵中的一个随机化 1000 次,然后他们比较这些一致性矩阵的值高于第一次比较的次数。
# Exemple:
mat1 <- matrix(rbinom(222, 1, 0.5),nrow=74,ncol=3) # habitat
mat2 <- matrix(rbinom(592, 1, 0.5),nrow=74,ncol=8) # modular
tmat1 <- t(mat1)
# Multipying the tranposed matrix 1 by matrix 2:
c <- tmat1%*%mat2 # concordance matrix c
resu <- matrix(NA,nrow=3,ncol=8) # creating the matrix to fill with the results of values higher than c
for(r in 1:1000) {
mat3 <- apply(mat1,1,sample) # randomizing matrix 1, maintaining the number of intercations by nodes
cc <- mat3%*%mat2 # multiplying these matrices
for (i in 1:dim(c)[1]){ # rows
for(j in 1:dim(c)[2]){ # columns
if(cc[i]>=c[j]){ #
resu[i,j] <- sum(cc[i]>=c[j]) # filling the matrix with the number of times cc was higher then c
}
}
}
}
但结果是错误的:它生成的矩阵具有正确的行数 (3) 和列数 (8),但全部填充为 1。
每个元素的一致性在 cc[i] 中高于 c[j] 的次数必须不同。
有什么想法吗?
【问题讨论】: