我在R中有这个过程:
M=4
n=3
number = runif(M,0,1)
label =LETTERS[1:M]
res <- xtabs(data.frame(c(number),c(label)))
sample <- sample(res,choose(n,2), replace = TRUE)
prob.matrix <- matrix(0, nrow = 3, ncol = 3)
prob.matrix[upper.tri(prob.matrix )]<- sample
,得到被采样的概率三角矩阵。例如,如果有:
> res
c.label.
A B C D
0.1668435 0.6432194 0.9573289 0.3870988
> sample
c.label.
C C A
0.9573289 0.9573289 0.1668435
> prob.matrix
[,1] [,2] [,3]
[1,] 0 0.9573289 0.9573289
[2,] 0 0.0000000 0.1668435
[3,] 0 0.0000000 0.0000000
除了prob.matrix之外,我还希望有一个标签矩阵来显示每个概率的标签,例如:
> label.matrix
[,1] [,2] [,3]
[1,] 0 C C
[2,] 0 0 A
[3,] 0 0 0
我应该如何获取label.matrix?
答案 0 :(得分:1)
也许您可以尝试下面的代码
label.matrix <- prob.matrix
label.matrix[label.matrix!=0] <- names(sample)
或
label.matrix <- prob.matrix
label.matrix[upper.tri(label.matrix)] <- names(sample)