结合矩阵和假名

时间:2018-08-10 09:17:10

标签: r matrix permutation

我是R的初学者。我尝试将一系列矩阵与一系列暗号相结合,以获得一组排列。我选择通过expand.grid构建一些东西,每个子集包含一个矩阵和一个暗名。

val x = "abc".r
val y = "abc".r
x == y
// res0: Boolean = false

这很好用,但是现在,如何使用函数和lapply组装具有每个暗名的每个矩阵,这将对每一行执行以下操作:

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)

最后,我应该得到这样的东西:

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)

我无法找到的是如何设置一个将在每个步骤中递增的变量,就像这里的x一样:

 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

谢谢

2 个答案:

答案 0 :(得分:1)

您在正确的轨道上:

  

“我无法找到的是如何设置一个将在每个步骤中递增的变量,例如x此处”

您可以使用lapply获得所需的输出:

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

这里是5x5矩阵的示例。首先,我们进行设置:

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)

现在,输出:

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 :(得分:1)

感谢约瑟夫找到原理。它可以与5 * 5矩阵和2 * 2排列配合使用。只需添加一点点,它就可以扩展到我的真实东西,大约是5 * 5矩阵的8 * 10排列。唯一要做的就是在应用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