如何求R中不同维度的多个矩阵的和

如何求R中不同维度的多个矩阵的和,r,R,我试图在R中添加两个维度不同的矩阵。理想情况下,系统应该将添加缺失行/列的两个矩阵等同起来,并用零填充。例如,如果一个矩阵有1:4行,另一个矩阵有1:5行,它们的列数是相同的。因此,要添加这两个矩阵,我们需要将第五行的零添加到第一个矩阵。 你能帮忙吗 #Matrix1 a11<-matrix(c(419371623, 10990236, 29346292, 0, 0, 39386246.52, 0, 0,0 ,0,0, 0, 0, 0, 30174248.77

我试图在R中添加两个维度不同的矩阵。理想情况下,系统应该将添加缺失行/列的两个矩阵等同起来,并用零填充。例如,如果一个矩阵有1:4行,另一个矩阵有1:5行,它们的列数是相同的。因此,要添加这两个矩阵,我们需要将第五行的零添加到第一个矩阵。
你能帮忙吗

#Matrix1 
a11<-matrix(c(419371623,    10990236,   29346292,   0,  0, 39386246.52, 0, 0,0  ,0,0,   0,  0,  0,  30174248.77,0,  27839925.91,    0   ,0  ,112921829.5),4,5,dimnames = list(c(1,2,3,5),c(1,2,3,4,5)),byrow=TRUE)

#Matrix 2    
a22<-matrix(c(853624485,    0, 766111,0, 0, 20240075.89 ,0, 4839059.2,0,    2062687.122 ,0, 0,0 ,0  ,0  ,7282484.458,0, 18738621.67 ,0  ,0),5,4,byrow=TRUE, list(c(1:5),c(1:4)))

#Expected Result:
res<- matrix(c(1272996108,  10990236,   30112402.72,    0,  0,
               39386247,    20240075.89,    0,  48390599.21,    0,
                      0,    2062687.122 ,0, 0,  30174249,
                      0,    27839926,   0,  7282484.458,    112921830,
                      0,    18738622,   0,  0,  0), 5, 5, byrow=TRUE, dimnames = list(1:5,1:5)
#矩阵1

a11您可以编写自己的函数来执行此操作:

`%+%` <- function(a, b){
  i <- dim(a)
  j <- dim(b)
  out <- pmax(i,j)
  valid <- pmin(i,j)
  result <- matrix(0, out[1], out[2])
  
  v_row <- seq(valid[1])
  v_col <- seq(valid[2])
  
  result[v_row, v_col] <- a[v_row, v_col] + b[v_row, v_col]

  ind1 <- which(result[seq(i[1]), seq(i[2])] == 0 & a!=0, TRUE)
  result[ind1] <- a[ind1]
  
  ind2 <- which(result[seq(j[1]), seq(j[2])] == 0 & b!=0, TRUE)
  result[ind2] <- b[ind2]
  
  result
}


a11%+%a22
           [,1]     [,2]     [,3]    [,4]      [,5]
[1,] 1272996108 10990236 30112403       0         0
[2,]   39386247 20240076        0 4839059         0
[3,]          0  2062687        0       0  30174249
[4,]          0 27839926        0 7282484 112921830
[5,]          0 18738622        0       0         0


s1 <- matrix(1,3,5)
s2 <- matrix(2, 5,2)
s1
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    1    1    1    1
[2,]    1    1    1    1    1
[3,]    1    1    1    1    1
s2
     [,1] [,2]
[1,]    2    2
[2,]    2    2
[3,]    2    2
[4,]    2    2
[5,]    2    2
s1 %+% s2
     [,1] [,2] [,3] [,4] [,5]
[1,]    3    3    1    1    1
[2,]    3    3    1    1    1
[3,]    3    3    1    1    1
[4,]    2    2    0    0    0
[5,]    2    2    0    0    0

`%+%`也许有一种更短/更好的方法使两个矩阵的维数相等,但这里有一种方法

n <- max(ncol(a11), ncol(a22))
m <- max(nrow(a11), nrow(a22))

if(m > ncol(a11)) a11 <- cbind(a11, matrix(0, ncol = m - ncol(a11), nrow = nrow(a11)))
if(m > ncol(a22)) a22 <- cbind(a22, matrix(0, ncol = m - ncol(a22), nrow = nrow(a22)))
if(n > nrow(a11)) a11 <- rbind(a11, matrix(0, ncol = ncol(a11), nrow = n - nrow(a11)))
if(n > nrow(a22)) a11 <- rbind(a11, matrix(0, ncol = ncol(a12), nrow = n - nrow(a22)))

在将res初始化为零之后,我通过索引添加矩阵

易于扩展以添加更多矩阵

# initilize res
res <- matrix(0, nrow = max(nrow(a11), nrow(a22)), 
        ncol = max(ncol(a11), ncol(a22)))   

# index of a11
inds <- which(!is.na(a11), arr.ind = T)
res[inds] <- res[inds] + a11[inds]

# index of a22
inds <- which(!is.na(a22), arr.ind = T)
res[inds] <- res[inds] + a22[inds]

# repeat for a33 etc if there's more.

这回答了你的问题吗?
# initilize res
res <- matrix(0, nrow = max(nrow(a11), nrow(a22)), 
        ncol = max(ncol(a11), ncol(a22)))   

# index of a11
inds <- which(!is.na(a11), arr.ind = T)
res[inds] <- res[inds] + a11[inds]

# index of a22
inds <- which(!is.na(a22), arr.ind = T)
res[inds] <- res[inds] + a22[inds]

# repeat for a33 etc if there's more.
> res
           [,1]     [,2]     [,3]    [,4]      [,5]
[1,] 1272996108 10990236 30112403       0         0
[2,]   39386247 20240076        0 4839059         0
[3,]          0  2062687        0       0  30174249
[4,]          0 27839926        0 7282484 112921830
[5,]          0 18738622        0       0         0