用于从其他 2 个矩阵索引重组矩阵的 for 循环

for loop for recomposing matrix from 2 others matrix indexes

我有两个行数和列数相同的矩阵,我想按它们的索引合并它们以创建一个新矩阵(我不知道 nrow() 和 ncol() 在advance, nrow() 来自 k kmeans 聚类质心和 ncol() 来自 k' knn 值)

A <- matrix(sample(letters), ncol = 10, nrow = 3)
B <- matrix(sample(letters), ncol = 10, nrow = 3)

一个

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "h"  "p"  "j"  "w"  "z"  "e"  "q"  "o"  "s"  "y"  
[2,] "y"  "b"  "k"  "t"  "a"  "v"  "f"  "x"  "c"  "r"  
[3,] "r"  "i"  "m"  "g"  "d"  "n"  "l"  "u"  "h"  "p"  

B

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]      
[1,] "k"  "q"  "l"  "n"  "o"  "r"  "u"  "b"  "s"  "y"       
[2,] "y"  "f"  "v"  "c"  "t"  "w"  "h"  "a"  "d"  "x"       
[3,] "x"  "e"  "j"  "g"  "m"  "i"  "p"  "z"  "k"  "q"       

我找到他们的索引:

a <- which(A !=0, arr.ind = T)
b <- which(B !=0, arr.ind = T)

我想要一个按行和按列索引合并 A 和 B 的最终矩阵,这样 A[1,1] 就在 B[1,1]

之前
A[1,1]  B[1,1]  A[1,2]  B[1,2]  A[1,3]  B[1,3]  A[1,4]  B[1,4]  ...
A[2,1]  B[2,1]  A[2,2]] B[2,2]  A[2,3]  B[2,3]  A[2,4]  B[2,4]  ...
A[3,1]  B[3,1]  A[3,2]  B[3,2]  A[3,3]  B[3,3]  A[3,4]  B[3,4]  ...

所以例如第一行是:

h   k   p   q   j   l   w   n   z   o

我发现 here lapply 函数完成了这项工作,但它给了我一个列表:

t <- lapply(1:length(knn.mat),
        function(i){cbind(A[i], B[i])})

我不能直接取消列表,因为我事先不知道我的输入矩阵会有多少行和列, 我想要一个矩阵或数据框作为输出,也许是带有 for 循环的东西,我可以将它与 apply 系列的函数一起使用? (这个不 运行 好)

doMat <- function(x,y){
  X <- matrix(0, nrow = nrow(x), ncol = ncol(x)*2)
  for (i in 1:nrow(x))
 {
    X[i] <- cbind(x[i],y[i])
    i = i+1
  }
  return(X)}

这将在一行中完成 - 利用 R 将矩阵视为向量的方式。

AB <- matrix(matrix(c(t(A),t(B)),nrow=2,byrow=TRUE),ncol=2*ncol(A),byrow=TRUE)

A
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "x"  "a"  "u"  "t"  "r"  "g"  "z"  "d"  "l"  "v"  
[2,] "v"  "h"  "n"  "w"  "i"  "b"  "k"  "o"  "y"  "m"  
[3,] "m"  "q"  "p"  "c"  "e"  "j"  "f"  "s"  "x"  "a"  

B
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "v"  "e"  "n"  "j"  "w"  "h"  "d"  "m"  "z"  "p"  
[2,] "p"  "g"  "t"  "a"  "f"  "r"  "i"  "s"  "q"  "c"  
[3,] "c"  "y"  "o"  "u"  "l"  "k"  "b"  "x"  "v"  "e"  

AB
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20]
[1,] "x"  "v"  "a"  "e"  "u"  "n"  "t"  "j"  "r"  "w"   "g"   "h"   "z"   "d"   "d"   "m"   "l"   "z"   "v"   "p"  
[2,] "v"  "p"  "h"  "g"  "n"  "t"  "w"  "a"  "i"  "f"   "b"   "r"   "k"   "i"   "o"   "s"   "y"   "q"   "m"   "c"  
[3,] "m"  "c"  "q"  "y"  "p"  "o"  "c"  "u"  "e"  "l"   "j"   "k"   "f"   "b"   "s"   "x"   "x"   "v"   "a"   "e" 

如果您要做的只是交织列,那么您可以使用与此问题类似的方法:Alternate, interweave or interlace two vectors

这似乎有效:matrix(c(rbind(A,B)), nrow = nrow(A))

编辑:

正如@Moody_Mudskipper 在评论中指出的那样,c() 在这种情况下实际上是多余的:matrix(rbind(A,B), nrow = nrow(A)) 也可以。

又拍了一张:)

A <- matrix(sample(letters), ncol = 10, nrow = 3)
B <- matrix(sample(letters), ncol = 10, nrow = 3)   

# > A
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# [1,] "h"  "f"  "g"  "q"  "e"  "s"  "a"  "b"  "v"  "i"  
# [2,] "i"  "o"  "t"  "y"  "r"  "z"  "u"  "x"  "w"  "l"  
# [3,] "l"  "c"  "m"  "n"  "k"  "p"  "j"  "d"  "h"  "f"

# > B
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# [1,] "b"  "p"  "e"  "g"  "x"  "q"  "k"  "o"  "r"  "d"  
# [2,] "d"  "j"  "n"  "v"  "w"  "l"  "t"  "a"  "c"  "f"  
# [3,] "f"  "u"  "z"  "m"  "h"  "s"  "y"  "i"  "b"  "p"

column_order <-rep(seq(1:ncol(A)),each=2) + rep(c(0,ncol(A)),times=ncol(A)) # 1,11,2,12 ...
AB_sorted  <- cbind(A,B)[,column_order]

# > AB_sorted
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20]
# [1,] "p"  "f"  "u"  "g"  "n"  "t"  "q"  "x"  "m"  "n"   "k"   "w"   "h"   "e"   "l"   "a"   "w"   "p"   "x"   "o"  
# [2,] "x"  "o"  "e"  "m"  "o"  "s"  "g"  "i"  "r"  "j"   "t"   "k"   "d"   "z"   "c"   "q"   "i"   "h"   "v"   "l"  
# [3,] "v"  "l"  "j"  "y"  "a"  "r"  "y"  "b"  "f"  "c"   "s"   "u"   "z"   "v"   "b"   "d"   "p"   "f"   "u"   "g"