向R中的矩阵添加列和行
所以我想做两个矩阵的点积,即使它们有不同的大小。问题是,如何将所有0的行和列附加到较小的矩阵中,使其与较大的矩阵大小相同向R中的矩阵添加列和行,r,R,所以我想做两个矩阵的点积,即使它们有不同的大小。问题是,如何将所有0的行和列附加到较小的矩阵中,使其与较大的矩阵大小相同 例如,如果我有一个2x2矩阵和一个4x4矩阵,我想看看在R中是否有一种方法来编码所有0的2行和2列的相加。有人能帮忙吗?假设你从两个矩阵开始m1和m2,其中较大的是m1 m1 <- matrix(1, 4, 4) m2 <- matrix(1, 2, 2) m1可能不是最优雅的方式,但我做了一个类似的任务,就是先水平添加填充矩阵,然后垂直添加填充矩阵,以完成缺少
例如,如果我有一个2x2矩阵和一个4x4矩阵,我想看看在R中是否有一种方法来编码所有0的2行和2列的相加。有人能帮忙吗?假设你从两个矩阵开始
m1
和m2
,其中较大的是m1
m1 <- matrix(1, 4, 4)
m2 <- matrix(1, 2, 2)
m1可能不是最优雅的方式,但我做了一个类似的任务,就是先水平添加填充矩阵,然后垂直添加填充矩阵,以完成缺少的部分,使其可组装。下面是一个函数和一个示例:
set.seed(1)
a <- matrix(rnorm(8), nrow=2, ncol=4)
b <- matrix(rnorm(10), nrow=5, ncol=2)
#> a
# [,1] [,2] [,3] [,4]
#[1,] -0.6264538 -0.8356286 0.3295078 0.4874291
#[2,] 0.1836433 1.5952808 -0.8204684 0.7383247
#> b
# [,1] [,2]
#[1,] 0.5757814 -2.21469989
#[2,] -0.3053884 1.12493092
#[3,] 1.5117812 -0.04493361
#[4,] 0.3898432 -0.01619026
#[5,] -0.6212406 0.94383621
# Here is a function that adds empty rows and columns of 0s to matrices x and y to make them comformable
# Two matrices x and y, and repeated fill element which is by default 0s
filler <- function(x, y, fill = 0){
# Block of x to add horizontally before vertical block
horizontal.x = matrix(fill,
ncol=ifelse(ncol(x) > ncol(y), 0, ncol(y) - ncol(x)),
nrow=nrow(x)
)
# Block of y to add horizontally before vertical block
horizontal.y = matrix(fill,
ncol=ifelse(ncol(x) > ncol(y), ncol(x) - ncol(y), 0),
nrow=nrow(y)
)
# Vertical block of x to add after the horizontal block
vertical.x = matrix(fill,
ncol=ncol(x) + ncol(horizontal.x),
nrow=ifelse(nrow(x) > nrow(y), 0, nrow(y) - nrow(x))
)
# Vertical block of y to add after the horizontal block
vertical.y = matrix(fill,
ncol=ncol(y) + ncol(horizontal.y),
nrow=ifelse(nrow(x) > nrow(y), nrow(x) - nrow(y), 0)
)
# Bind the blocks together and return both within a list
x <- rbind(cbind(x, horizontal.x), vertical.x)
y <- rbind(cbind(y, horizontal.y), vertical.y)
list(x, y)
}
filler(a, b)
filler(b, a)
filler(b, t(a))
#> filler(a, b)
#[[1]]
# [,1] [,2] [,3] [,4]
#[1,] -0.6264538 -0.8356286 0.3295078 0.4874291
#[2,] 0.1836433 1.5952808 -0.8204684 0.7383247
#[3,] 0.0000000 0.0000000 0.0000000 0.0000000
#[4,] 0.0000000 0.0000000 0.0000000 0.0000000
#[5,] 0.0000000 0.0000000 0.0000000 0.0000000
#
#[[2]]
# [,1] [,2] [,3] [,4]
#[1,] 0.5757814 -2.21469989 0 0
#[2,] -0.3053884 1.12493092 0 0
#[3,] 1.5117812 -0.04493361 0 0
#[4,] 0.3898432 -0.01619026 0 0
#[5,] -0.6212406 0.94383621 0 0
#
#> filler(b, a)
#[[1]]
# [,1] [,2] [,3] [,4]
#[1,] 0.5757814 -2.21469989 0 0
#[2,] -0.3053884 1.12493092 0 0
#[3,] 1.5117812 -0.04493361 0 0
#[4,] 0.3898432 -0.01619026 0 0
#[5,] -0.6212406 0.94383621 0 0
#
#[[2]]
# [,1] [,2] [,3] [,4]
#[1,] -0.6264538 -0.8356286 0.3295078 0.4874291
#[2,] 0.1836433 1.5952808 -0.8204684 0.7383247
#[3,] 0.0000000 0.0000000 0.0000000 0.0000000
#[4,] 0.0000000 0.0000000 0.0000000 0.0000000
#[5,] 0.0000000 0.0000000 0.0000000 0.0000000
#
#> filler(b, t(a))
#[[1]]
# [,1] [,2]
#[1,] 0.5757814 -2.21469989
#[2,] -0.3053884 1.12493092
#[3,] 1.5117812 -0.04493361
#[4,] 0.3898432 -0.01619026
#[5,] -0.6212406 0.94383621
#
#[[2]]
# [,1] [,2]
#[1,] -0.6264538 0.1836433
#[2,] -0.8356286 1.5952808
#[3,] 0.3295078 -0.8204684
#[4,] 0.4874291 0.7383247
#[5,] 0.0000000 0.0000000
ex <- filler(b, t(a))
# This is now defined
ex[[1]] %*% t(ex[[2]])
set.seed(1)
a b
# [,1] [,2]
#[1,] 0.5757814 -2.21469989
#[2,] -0.3053884 1.12493092
#[3,] 1.5117812 -0.04493361
#[4,] 0.3898432 -0.01619026
#[5,] -0.6212406 0.94383621
#这是一个函数,它将0的空行和空列添加到矩阵x和y中,以使它们可组合
#两个矩阵x和y,以及重复填充元素(默认为0s)
填料ncol(y),0,ncol(y)-ncol(x)),
nrow=nrow(x)
)
#要在垂直块之前水平添加的y块
水平。y=矩阵(填充,
ncol=ifelse(ncol(x)>ncol(y),ncol(x)-ncol(y),0),
nrow=nrow(y)
)
#要在水平块之后添加的x垂直块
垂直。x=矩阵(填充,
ncol=ncol(x)+ncol(水平x),
nrow=ifelse(nrow(x)>nrow(y),0,nrow(y)-nrow(x))
)
#在水平块之后添加y的垂直块
垂直。y=矩阵(填充,
ncol=ncol(y)+ncol(水平y),
nrow=ifelse(nrow(x)>nrow(y),nrow(x)-nrow(y),0)
)
#将块绑定在一起,并在列表中返回这两个块
x填料(b、a)
#[[1]]
# [,1] [,2] [,3] [,4]
#[1,] 0.5757814 -2.21469989 0 0
#[2,] -0.3053884 1.12493092 0 0
#[3,] 1.5117812 -0.04493361 0 0
#[4,] 0.3898432 -0.01619026 0 0
#[5,] -0.6212406 0.94383621 0 0
#
#[[2]]
# [,1] [,2] [,3] [,4]
#[1,] -0.6264538 -0.8356286 0.3295078 0.4874291
#[2,] 0.1836433 1.5952808 -0.8204684 0.7383247
#[3,] 0.0000000 0.0000000 0.0000000 0.0000000
#[4,] 0.0000000 0.0000000 0.0000000 0.0000000
#[5,] 0.0000000 0.0000000 0.0000000 0.0000000
#
#>填料(b、t(a))
#[[1]]
# [,1] [,2]
#[1,] 0.5757814 -2.21469989
#[2,] -0.3053884 1.12493092
#[3,] 1.5117812 -0.04493361
#[4,] 0.3898432 -0.01619026
#[5,] -0.6212406 0.94383621
#
#[[2]]
# [,1] [,2]
#[1,] -0.6264538 0.1836433
#[2,] -0.8356286 1.5952808
#[3,] 0.3295078 -0.8204684
#[4,] 0.4874291 0.7383247
#[5,] 0.0000000 0.0000000
前任
sameDim <- function(x, size, fill = 0L) {
rb <- rbind(x, matrix(fill, size[1]-nrow(x), ncol(x)))
cbind(rb, matrix(fill, nrow(rb), size[2]-ncol(x)))
}
sameDim(m2, dim(m1))
# [,1] [,2] [,3] [,4]
# [1,] 1 1 0 0
# [2,] 1 1 0 0
# [3,] 0 0 0 0
# [4,] 0 0 0 0
set.seed(1)
a <- matrix(rnorm(8), nrow=2, ncol=4)
b <- matrix(rnorm(10), nrow=5, ncol=2)
#> a
# [,1] [,2] [,3] [,4]
#[1,] -0.6264538 -0.8356286 0.3295078 0.4874291
#[2,] 0.1836433 1.5952808 -0.8204684 0.7383247
#> b
# [,1] [,2]
#[1,] 0.5757814 -2.21469989
#[2,] -0.3053884 1.12493092
#[3,] 1.5117812 -0.04493361
#[4,] 0.3898432 -0.01619026
#[5,] -0.6212406 0.94383621
# Here is a function that adds empty rows and columns of 0s to matrices x and y to make them comformable
# Two matrices x and y, and repeated fill element which is by default 0s
filler <- function(x, y, fill = 0){
# Block of x to add horizontally before vertical block
horizontal.x = matrix(fill,
ncol=ifelse(ncol(x) > ncol(y), 0, ncol(y) - ncol(x)),
nrow=nrow(x)
)
# Block of y to add horizontally before vertical block
horizontal.y = matrix(fill,
ncol=ifelse(ncol(x) > ncol(y), ncol(x) - ncol(y), 0),
nrow=nrow(y)
)
# Vertical block of x to add after the horizontal block
vertical.x = matrix(fill,
ncol=ncol(x) + ncol(horizontal.x),
nrow=ifelse(nrow(x) > nrow(y), 0, nrow(y) - nrow(x))
)
# Vertical block of y to add after the horizontal block
vertical.y = matrix(fill,
ncol=ncol(y) + ncol(horizontal.y),
nrow=ifelse(nrow(x) > nrow(y), nrow(x) - nrow(y), 0)
)
# Bind the blocks together and return both within a list
x <- rbind(cbind(x, horizontal.x), vertical.x)
y <- rbind(cbind(y, horizontal.y), vertical.y)
list(x, y)
}
filler(a, b)
filler(b, a)
filler(b, t(a))
#> filler(a, b)
#[[1]]
# [,1] [,2] [,3] [,4]
#[1,] -0.6264538 -0.8356286 0.3295078 0.4874291
#[2,] 0.1836433 1.5952808 -0.8204684 0.7383247
#[3,] 0.0000000 0.0000000 0.0000000 0.0000000
#[4,] 0.0000000 0.0000000 0.0000000 0.0000000
#[5,] 0.0000000 0.0000000 0.0000000 0.0000000
#
#[[2]]
# [,1] [,2] [,3] [,4]
#[1,] 0.5757814 -2.21469989 0 0
#[2,] -0.3053884 1.12493092 0 0
#[3,] 1.5117812 -0.04493361 0 0
#[4,] 0.3898432 -0.01619026 0 0
#[5,] -0.6212406 0.94383621 0 0
#
#> filler(b, a)
#[[1]]
# [,1] [,2] [,3] [,4]
#[1,] 0.5757814 -2.21469989 0 0
#[2,] -0.3053884 1.12493092 0 0
#[3,] 1.5117812 -0.04493361 0 0
#[4,] 0.3898432 -0.01619026 0 0
#[5,] -0.6212406 0.94383621 0 0
#
#[[2]]
# [,1] [,2] [,3] [,4]
#[1,] -0.6264538 -0.8356286 0.3295078 0.4874291
#[2,] 0.1836433 1.5952808 -0.8204684 0.7383247
#[3,] 0.0000000 0.0000000 0.0000000 0.0000000
#[4,] 0.0000000 0.0000000 0.0000000 0.0000000
#[5,] 0.0000000 0.0000000 0.0000000 0.0000000
#
#> filler(b, t(a))
#[[1]]
# [,1] [,2]
#[1,] 0.5757814 -2.21469989
#[2,] -0.3053884 1.12493092
#[3,] 1.5117812 -0.04493361
#[4,] 0.3898432 -0.01619026
#[5,] -0.6212406 0.94383621
#
#[[2]]
# [,1] [,2]
#[1,] -0.6264538 0.1836433
#[2,] -0.8356286 1.5952808
#[3,] 0.3295078 -0.8204684
#[4,] 0.4874291 0.7383247
#[5,] 0.0000000 0.0000000
ex <- filler(b, t(a))
# This is now defined
ex[[1]] %*% t(ex[[2]])