R 用交替列组合矩阵

R 用交替列组合矩阵,r,matrix,R,Matrix,我正在寻找一种综合两个矩阵的通用方法,以便两个初始矩阵中的列在新矩阵中交替 col1m1…col1m2…col2m1…col2m2…col3m1…col3m2 例如: matrix.odd <- matrix(c(rep(1,3),rep(3,3),rep(5,3)),nrow=3,ncol=3) matrix.even <- matrix(c(rep(2,3),rep(4,3),rep(6,3)),nrow=3,ncol=3) # would look like matrix.c

我正在寻找一种综合两个矩阵的通用方法,以便两个初始矩阵中的列在新矩阵中交替

col1m1…col1m2…col2m1…col2m2…col3m1…col3m2

例如:

matrix.odd  <- matrix(c(rep(1,3),rep(3,3),rep(5,3)),nrow=3,ncol=3)
matrix.even <- matrix(c(rep(2,3),rep(4,3),rep(6,3)),nrow=3,ncol=3)
# would look like
matrix.combined <- matrix(c(rep(1,3),rep(2,3),rep(3,3),rep(4,3),rep(5,3),rep(6,3)),
                          nrow=3,ncol=6)

matrix.odd
rows.combined可能有一种更简洁的方法。如果矩阵较大,则可能需要寻找更有效的方法

# Test data
(X <- matrix(1:16, nrow=4, ncol=4))
(Y <- matrix(-16:-1, nrow=4, ncol=4))

# Set indices for the new matrix
X.idx <- seq(1, ncol(X)*2, by=2)
Y.idx <- seq(2, ncol(Y)*2+1, by=2)

# Column bind the matrices and name columns according to the indices
XY <- cbind(X, Y)
colnames(XY) <- c(X.idx, Y.idx)

# Now order the columns
XY[, order(as.numeric(colnames(XY)))]
测试数据
(XSmth这样做应该:

m <- cbind(matrix.odd, matrix.even)                   # combine
m <- m[, c(matrix(1:ncol(m), nrow = 2, byrow = T))]   # then reorder
要玩多矩阵游戏:

weave = function(...) {
  l = list(...)
  matrix(do.call(rbind, l), nrow = nrow(l[[1]]))
}
很容易推广到任意数量的矩阵:

alternate.cols <- function(...) {
  l <- list(...)
  m <- do.call(cbind, l)
  i <- order(sequence(sapply(l, ncol)))
  m[, i]
}

alternate.cols您可以将其转换为3D数组,然后转置

arr <- array( c(m1,m2) , dim = c(dim(m1),2) )
matrix( aperm( arr , c(1,3,2) ) , nrow(m1) )
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    2    3    4    5    6
[2,]    1    2    3    4    5    6
[3,]    1    2    3    4    5    6

arr+1很好的解决方案。但是我建议指定
byrow=TRUE
作为用户可以反弹的
T
。@eddi您能告诉我如何调整此选项以进行行组合吗?谢谢。@Anusha
matrix(T(cbind(matrix.odd,matrix.偶数)),ncol=ncol(matrix.odd),byrow=T)
可能吗?@eddi谢谢。我尝试了bycol,但我认为它没有定义。基准测试,我发现你的第一个建议是最快的解决方案,与@flodel差不多。相比之下,你的“其他乐趣选项”似乎慢了20%。(Zero的解决方案花费的时间不到两倍,Simon的解决方案花费的时间不到两倍,但这稍微取决于矩阵的形状。)谢谢。我试着用更大一点的矩阵集(每个矩阵5列)来实现这一点。在最后一个命令对列进行排序之前,一切都进行得很顺利。s我得到的排序是1,10,2,3,4,5,6,7,8,9。这是否需要一个“as.numeric”参数?我会尝试一些方法。你是对的。我会更改它,尽管eddi的解决方案可能更好。@zero323我尝试通过执行matrix.combined(seq)对行进行调整(1,rows.combined,2),]但它添加了第一个矩阵,而不是第二个矩阵。如何将其用于行?
alternate.cols <- function(m1, m2) {
  cbind(m1, m2)[, order(c(seq(ncol(m1)), seq(ncol(m2))))]
}

identical(matrix.combined, alternate.cols(matrix.odd, matrix.even))
# [1] TRUE
alternate.cols(matrix.odd, matrix.even[, -3])
#      [,1] [,2] [,3] [,4] [,5]
# [1,]    1    2    3    4    5
# [2,]    1    2    3    4    5
# [3,]    1    2    3    4    5
alternate.cols <- function(...) {
  l <- list(...)
  m <- do.call(cbind, l)
  i <- order(sequence(sapply(l, ncol)))
  m[, i]
}
arr <- array( c(m1,m2) , dim = c(dim(m1),2) )
matrix( aperm( arr , c(1,3,2) ) , nrow(m1) )
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    2    3    4    5    6
[2,]    1    2    3    4    5    6
[3,]    1    2    3    4    5    6
bindR <- function(...){
    args <- list(...)
    dims <- c( dim(args[[1]]) , length(args) )
    arr <- array( unlist( args ) , dim = dims )
    matrix( aperm( arr , c(1,3,2) ) , dims[1] )
}


bindR(m1,m2,m1,m2)
#     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
#[1,]    1    2    1    2    3    4    3    4    5     6     5     6
#[2,]    1    2    1    2    3    4    3    4    5     6     5     6
#[3,]    1    2    1    2    3    4    3    4    5     6     5     6