我正在尝试合并两个邻接矩阵,而忽略对称交点。
M1<-matrix(c(0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0),nrow=5,ncol=5,byrow=T)
M2<-matrix(c(0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0),nrow=5,ncol=5,byrow=T)
问题是:如何到达下面的矩阵“忘记” ([1,2],[2,1])
,并仅显示一个和一个零?
我的最终结果应该是:
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 0 0
[2,] 0 0 0 0 0
[3,] 1 0 0 0 0
[4,] 1 0 0 0 1
[5,] 0 0 0 0 0
我已经尝试了涉及t(M2)的各种加法和减法,但是总有问题。
答案 0 :(得分:3)
M = M1+M2
M[M==t(M)]=0
+(M>0)
[,1] [,2] [,3] [,4] [,5]
[1,] 0 0 0 0 0
[2,] 0 0 0 0 0
[3,] 1 0 0 0 0
[4,] 1 0 0 0 1
[5,] 0 0 0 0 0
答案 1 :(得分:0)
感谢Onyambu。将您的解决方案应用于我的MClist
的40(Mn,Nn)对中,似乎可以这种方式工作:
AddMC<-lapply(1:40, function(x){
(MClist[[x]][[1]]+MClist[[x]][[2]])
})
InterMC<-lapply(1:40, function(x){
AddMC[[x]][AddMC[[x]]==t(AddMC[[x]])]=0
+(AddMC[[x]]>0)
})