1
votes

I'm a beginner i n R. I try to combine a series of matrices with a series of dimnames to obtain a set of permutations. I choosed to build something by expand.grid with each subset containing one matrix and one dimnames.

M1=matrix(c(0,1,0,1,0,0,0,0,1), nrow=3,ncol=3,byrow=T)
M2=matrix(c(0,0,1,0,1,0,1,0,0), nrow=3,ncol=3,byrow=T)
Mlist<-list(M1,M2)

Na<-list(c("A","B","C"),c("A","B","C"))
Nb<-list(c("B","A","C"),c("B","A","C"))
Nlist<-list(Na,Nb)
M<-expand.grid(Nlist,Mlist)

This works fine, but now, how to assemble each matrix with each dimnames with a function and lapply that will do the following for each row :

Mat1<-matrix(M[[2]][[1]],dimnames=list(M[[1]][[1]][[1]],M[[1]][[1]][[2]]),nrow=3,ncol=3,byrow=T)
Mat2<-matrix(M[[2]][[2]],dimnames=list(M[[1]][[2]][[1]],M[[1]][[2]][[1]]),nrow=3,ncol=3,byrow=T)

At the end, I should get something like this :

 M1
  A B C
A 0 1 0
B 1 0 0
C 0 0 1
 M2
  B A C
B 0 1 0
A 1 0 0
C 0 0 1
 M3
  A B C
A 0 0 1
B 0 1 0
C 1 0 0
 M4
  B A C
B 0 0 1
A 0 1 0
C 1 0 0

What I'm unable to find is how to set a variable that will be incremented at each step, something like x here :

Mat<-matrix(M[[2]][[x]],dimnames=list(M[[1]][[x]][[1]],M[[1]][[x]][[2]]),nrow=3,ncol=3,byrow=T)

Thank you

2

2 Answers

1
votes

You are on the right track:

"What I'm unable to find is how to set a variable that will be incremented at each step, something like x here"

You can use lapply to get the desired output:

lapply(1:4, function(x) {
    matrix(M[[2]][[x]],dimnames=list(M[[1]][[x]][[1]],M[[1]][[x]][[2]]),nrow=3,ncol=3,byrow=T)
})

[[1]]
  A B C
A 0 1 0
B 1 0 0
C 0 0 1

[[2]]
  B A C
B 0 1 0
A 1 0 0
C 0 0 1

[[3]]
  A B C
A 0 0 1
B 0 1 0
C 1 0 0

[[4]]
  B A C
B 0 0 1
A 0 1 0
C 1 0 0

Here is an example with 5x5 matrices. First we have the setup:

M1 <- matrix(0, nrow=5,ncol=5)
diag(M1) <- 1L
M2 <- matrix(0, nrow=5,ncol=5)

j <- 5L
for (i in 1:5) {
    M2[i, j] <- 1L
    j <- j - 1L
}

Mlist<-list(M1,M2)
Na<-list(LETTERS[1:5], LETTERS[1:5])
Nb<-list(LETTERS[c(2,1,3:5)], LETTERS[c(2,1,3:5)])
Nlist<-list(Na,Nb)
M<-expand.grid(Nlist,Mlist)

And now, the output:

lapply(1:4, function(x) {
    matrix(M[[2]][[x]],dimnames=list(M[[1]][[x]][[1]],M[[1]][[x]][[2]]),nrow=5,ncol=5,byrow=T)
})

[[1]]
  A B C D E
A 1 0 0 0 0
B 0 1 0 0 0
C 0 0 1 0 0
D 0 0 0 1 0
E 0 0 0 0 1

[[2]]
  B A C D E
B 1 0 0 0 0
A 0 1 0 0 0
C 0 0 1 0 0
D 0 0 0 1 0
E 0 0 0 0 1

[[3]]
  A B C D E
A 0 0 0 0 1
B 0 0 0 1 0
C 0 0 1 0 0
D 0 1 0 0 0
E 1 0 0 0 0

[[4]]
  B A C D E
B 0 0 0 0 1
A 0 0 0 1 0
C 0 0 1 0 0
D 0 1 0 0 0
E 1 0 0 0 0
1
votes

Thanks to Joseph for finding the principle. It works fine with 5*5 matrices and 2*2 permutations. And with just a little addition it could be extended to my real stuff which is about 8*10 permutations of 5*5 matrices. The only thing which had to be done is to transpose the matrices before applying expand.grid.

#J Wood solution with my 10 nodes and 8 matrices
M1<-matrix(c(0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0), nrow=5,ncol=5,byrow=T)
M2<-matrix(c(0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0), nrow=5,ncol=5,byrow=T)
M3<-matrix(c(0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0), nrow=5,ncol=5,byrow=T)
M4<-matrix(c(0,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), nrow=5,ncol=5,byrow=T)
M5<-matrix(c(0,0,0,0,0,1,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0), nrow=5,ncol=5,byrow=T)
M6<-matrix(c(0,0,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0), nrow=5,ncol=5,byrow=T)
M7<-matrix(c(0,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), nrow=5,ncol=5,byrow=T)
M8<-matrix(c(0,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0), nrow=5,ncol=5,byrow=T)
tMlist<-list(t(M1),t(M2),t(M3),t(M4),t(M5),t(M6),t(M7),t(M8))

N1<-list(c("A","B","C","D","E"),c("A","B","C","D","E"))
N2<-list(c("A","B","D","C","E"),c("A","B","D","C","E"))
N3<-list(c("A","B","E","C","D"),c("A","B","E","C","D"))
N4<-list(c("A","C","D","B","E"),c("A","C","D","B","E"))
N5<-list(c("A","C","E","B","D"),c("A","C","E","B","D"))
N6<-list(c("A","D","E","B","C"),c("A","D","E","B","C"))
N7<-list(c("B","C","D","A","E"),c("B","C","D","A","E"))
N8<-list(c("B","C","E","A","D"),c("B","C","E","A","D"))
N9<-list(c("B","D","E","A","C"),c("B","D","E","A","C"))
N10<-list(c("C","D","E","A","B"),c("C","D","E","A","B"))
Nlist<-list(N1,N2,N3,N4,N5,N6,N7,N8,N8,N10)

M<-expand.grid(Nlist,tMlist)
lapply(1:80, function(x) {
  matrix(M[[2]][[x]],dimnames=list(M[[1]][[x]][[1]],M[[1]][[x]][[2]]),nrow=5,ncol=5,byrow=T)
})

[[1]]
  A B C D E
A 0 1 0 0 0
B 0 0 1 0 0
C 1 0 0 0 0
D 0 0 0 0 0
E 0 0 0 0 0

[[2]]
  A B D C E
A 0 1 0 0 0
B 0 0 1 0 0
D 1 0 0 0 0
C 0 0 0 0 0
E 0 0 0 0 0 
.....
[[80]]
  C D E A B
C 0 1 0 0 0
D 0 0 0 0 0
E 1 1 0 0 0
A 0 0 0 0 0
B 0 0 0 0 0