Create a larger matrix from a smaller size

I have a matrix A, which is:

A <- matrix(c(1:15), byrow=T, nrow=5)
A
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9
[4,]   10   11   12
[5,]   13   14   15

Now I want to create a matrix B, size 8x8 (or 10x10 or 15x15, etc.), which will look like this:

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    2    3    0    0    0    0    0
[2,]    4    5    6    0    0    0    0    0
[3,]    7    8    9    0    0    0    0    0
[4,]   10   11   12    0    0    0    0    0
[5,]   13   14   15    0    0    0    0    0
[6,]    0    0    0    0    0    0    0    0
[7,]    0    0    0    0    0    0    0    0
[8,]    0    0    0    0    0    0    0    0

So, starting with A, I want to add 8x8 columns and rows, all are replaced with zero values ​​... Any ideas? Thanks in advance!

+3
source share
3 answers

Try assuming you Ahave at least one row and one column:

B <- matrix(0, 8, 8)
B[1:nrow(A), 1:ncol(A)] <- A

or as one operator:

B <- "[<-"(matrix(0, 8, 8), 1:nrow(A), 1:ncol(A), value = A)

If you Acan have zero or null columns, instead 1:nrow(A), and 1:ncol(A)use the seq_len(nrow(A))and seq_len(ncol(A)).

Alternatively, this works even if A has zero rows or columns:

B <- matrix(0, 8, 8)
B[cbind(c(row(A)), c(col(A)))] <- A

or

B <- "[<-"(matrix(0, 8, 8), cbind(c(row(A)), c(col(A))), value = A)

or

B <- replace(matrix(0, 8, 8), cbind(c(row(A)), c(col(A))), A)
+3

- , , :

    expandR = function(m,nrows,ncols,with=0){
      p=matrix(with,nrows,ncols)
      p[1:nrow(m),1:ncol(m)]=m
      p
    }

, (, ..). , , .

+6

What about:

A <- matrix(c(1:15), byrow=T, nrow=5)

expandMatrix <- function(X, nrow, ncol) {
    X <- cbind(X, matrix(0, nrow = nrow(X), ncol = ncol - ncol(X)))
    X <- rbind(X, matrix(0, nrow = nrow - nrow(X), ncol = ncol(X)))
    X
}

Then

> expandMatrix(A, 8, 8)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    2    3    0    0    0    0    0
[2,]    4    5    6    0    0    0    0    0
[3,]    7    8    9    0    0    0    0    0
[4,]   10   11   12    0    0    0    0    0
[5,]   13   14   15    0    0    0    0    0
[6,]    0    0    0    0    0    0    0    0
[7,]    0    0    0    0    0    0    0    0
[8,]    0    0    0    0    0    0    0    0

or

> expandMatrix(A, 10, 10)
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1    2    3    0    0    0    0    0    0     0
 [2,]    4    5    6    0    0    0    0    0    0     0
 [3,]    7    8    9    0    0    0    0    0    0     0
 [4,]   10   11   12    0    0    0    0    0    0     0
 [5,]   13   14   15    0    0    0    0    0    0     0
 [6,]    0    0    0    0    0    0    0    0    0     0
 [7,]    0    0    0    0    0    0    0    0    0     0
 [8,]    0    0    0    0    0    0    0    0    0     0
 [9,]    0    0    0    0    0    0    0    0    0     0
[10,]    0    0    0    0    0    0    0    0    0     0

You can also specify it by default if you basically want the square matrix to be output:

expandMatrix <- function(X, nrow, ncol = nrow) {
    X <- cbind(X, matrix(0, nrow = nrow(X), ncol = ncol - ncol(X)))
    X <- rbind(X, matrix(0, nrow = nrow - nrow(X), ncol = ncol(X)))
    X
}

Then it expandMatrix(A, 8)will be enough.

+1
source

Source: https://habr.com/ru/post/1769859/


All Articles