0
votes

So I want to do the matrix dot product with two matrices, even if they have different sizes. The problem is, how do I append rows and columns of all 0s to the smaller matrix to make it the same size as the larger one.

So for example, if I have a 2x2 matrix and a 4x4 matrix, I want to see if there's a way in R to code the addition of 2 rows and 2 columns of all 0s. Can someone help out?

2

2 Answers

0
votes

Probably not the most elegant way, but I've done a similar task just by adding filler matrices first horizontally and then vertically to fulfill the lacking part to make them comformable. Here's a function as well as an example:

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]])
0
votes

Suppose you start with two matrices m1 and m2, the bigger being m1

m1 <- matrix(1, 4, 4)
m2 <- matrix(1, 2, 2)

You can create a third matrix of zeros with the same dimensions as the bigger matrix, then fill in the smaller matrix values.

m3 <- matrix(0, dim(m1)[1], dim(m1)[2])
m3[1:nrow(m2), 1:ncol(m2)] <- m2
m3
#      [,1] [,2] [,3] [,4]
# [1,]    1    1    0    0
# [2,]    1    1    0    0
# [3,]    0    0    0    0
# [4,]    0    0    0    0

Here's a possible function that you could use to make a square matrix based on a given set of dimensions.

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