SA12
SA12

Reputation: 318

Creating a probability matrix with its label matrix

I have this process in 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

and get a triangle matrix of probability which were sampling. For example if there is:

> 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

more than prob.matrix, I want to have a label matrix that shows the label of each probability,such as:

> label.matrix
     [,1]      [,2]      [,3]
[1,]    0       C         C
[2,]    0       0         A
[3,]    0       0         0

What should i do for getting label.matrix?

Upvotes: 0

Views: 51

Answers (1)

ThomasIsCoding
ThomasIsCoding

Reputation: 101335

Maybe you can try the code below

label.matrix <- prob.matrix
label.matrix[label.matrix!=0] <- names(sample)

or

label.matrix <- prob.matrix
label.matrix[upper.tri(label.matrix)] <- names(sample)

Upvotes: 1

Related Questions