The main idea is how to generate matrices where the sum of each matches values is 1
. That is, I do not have matrices but would like to generate them based on this condition.
I aim to generate a list of lower.tri
matrices. The main trick here is how I can generate all these matrices where the sum of every match elements is 1
.
For example,
w1 <- c(0,0.7,0.8,0.5,0.2,
0,0,0.7,0.6,0.3,
0,0,0,0.9,0.8,
0,0,0,0,0.3,
0,0,0,0,0)
w1 <- matrix(w1,5,5)
w2 <- c(0,0.3,0.2,0.5,0.8,
0,0,0.3,0.4,0.7,
0,0,0,0.1,0.2,
0,0,0,0,0.7,
0,0,0,0,0)
w2 <- matrix(w2,5,5)
The main idea is:
w5 <- 1 - (w1+w2+w3+w4)
. w1 <- 1-w2
, w3 <- 1- (w1+w2)
. w4 <- 1- (w1+w2+w3)
.
Here we can see that the sum of each matches values is 1
.
For example:
> w1
[,1] [,2] [,3] [,4] [,5]
[1,] 0.0 0.0 0.0 0.0 0
[2,] 0.7 0.0 0.0 0.0 0
[3,] 0.8 0.7 0.0 0.0 0
[4,] 0.5 0.6 0.9 0.0 0
[5,] 0.2 0.3 0.8 0.3 0
> w2
[,1] [,2] [,3] [,4] [,5]
[1,] 0.0 0.0 0.0 0.0 0
[2,] 0.3 0.0 0.0 0.0 0
[3,] 0.2 0.3 0.0 0.0 0
[4,] 0.5 0.4 0.1 0.0 0
[5,] 0.8 0.7 0.2 0.7 0
w1[2,1] = 0.7
w2[2,1]= 0.3
Then their sum = 1
. The same for all w1[k,j] + w2[k,j]
.
The question:
How can I generate 5
matrices where the sum of each matches values is 1
. I was thinking about lapply
however, I really cannot get the idea for this part.
For example, The output should a list of 5 matrices. For example,
> w1
[,1] [,2] [,3] [,4] [,5]
[1,] 0.0 0.0 0.0 0.0 0
[2,] 0.5 0.0 0.0 0.0 0
[3,] 0.6 0.6 0.0 0.0 0
[4,] 0.5 0.6 0.7 0.0 0
[5,] 0.2 0.2 0.2 0.3 0
> w2
[,1] [,2] [,3] [,4] [,5]
[1,] 0.0 0.0 0.0 0.0 0
[2,] 0.2 0.0 0.0 0.0 0
[3,] 0.2 0.3 0.0 0.0 0
[4,] 0.5 0.2 0.1 0.0 0
[5,] 0.7 0.5 0.2 0.5 0
> w3
[,1] [,2] [,3] [,4] [,5]
[1,] 0.0 0.0 0.0 0.0 0
[2,] 0.1 0.0 0.0 0.0 0
[3,] 0.2 0.1 0.0 0.0 0
[4,] 0.3 0.2 0.3 0.0 0
[5,] 0.1 0.3 0.6 0.2 0