Adela
Adela

Reputation: 1797

Expand dimension of list of matrices

I have a list of matrices with different dimensions and I would like to create list of matrices where number of columns is the same as the maximal number of columns among list elements.

set.seed(1)

matrix.list <- list(matrix(rnorm(8), ncol = 2, nrow = 4),
                    matrix(rnorm(8), ncol = 4, nrow = 2),
                    matrix(rnorm(12), ncol = 3, nrow = 4))

matrix.list
[[1]]
           [,1]       [,2]
[1,] -0.6264538  0.3295078
[2,]  0.1836433 -0.8204684
[3,] -0.8356286  0.4874291
[4,]  1.5952808  0.7383247

[[2]]
           [,1]      [,2]       [,3]        [,4]
[1,]  0.5757814 1.5117812 -0.6212406  1.12493092
[2,] -0.3053884 0.3898432 -2.2146999 -0.04493361

[[3]]
            [,1]        [,2]        [,3]
[1,] -0.01619026  0.91897737  0.61982575
[2,]  0.94383621  0.78213630 -0.05612874
[3,]  0.82122120  0.07456498 -0.15579551
[4,]  0.59390132 -1.98935170 -1.47075238

Desired output should be:

[[1]]
           [,1]       [,2] [,3] [,4]
[1,] -0.6264538  0.3295078    0    0
[2,]  0.1836433 -0.8204684    0    0
[3,] -0.8356286  0.4874291    0    0
[4,]  1.5952808  0.7383247    0    0

[[2]]
           [,1]      [,2]       [,3]        [,4]
[1,]  0.5757814 1.5117812 -0.6212406  1.12493092
[2,] -0.3053884 0.3898432 -2.2146999 -0.04493361

[[3]]
            [,1]        [,2]        [,3] [,4]
[1,] -0.01619026  0.91897737  0.61982575    0
[2,]  0.94383621  0.78213630 -0.05612874    0
[3,]  0.82122120  0.07456498 -0.15579551    0
[4,]  0.59390132 -1.98935170 -1.47075238    0

My not a very elegant solution:

# dimensions of matrices
ncols <- sapply(matrix.list, ncol)
nrows <- sapply(matrix.list, nrow)

# max number of columns
max.ncol <- max(sapply(matrix.list, ncol))

# creating new matrix list
new.matrix.list <- lapply(1:length(matrix.list), function(i) 
  matrix(0, ncol = max.ncol, nrow = nrows[i]))
for (i in 1:length(matrix.list)){
  new.matrix.list[[i]][, 1:ncols[i]] <- matrix.list[[i]]
}

Upvotes: 2

Views: 285

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388992

One way could be to find max number of columns from the list. We then use lapply to create a new matrix with same number of rows as each matrix and column as difference of column with colmax.

colmax <-  max(sapply(matrix.list, ncol))
lapply(matrix.list, function(x) 
        cbind(x, matrix(0, ncol = colmax - ncol(x), nrow = nrow(x))))

#[[1]]
#           [,1]       [,2] [,3] [,4]
#[1,] -0.6264538  0.3295078    0    0
#[2,]  0.1836433 -0.8204684    0    0
#[3,] -0.8356286  0.4874291    0    0
#[4,]  1.5952808  0.7383247    0    0

#[[2]]
#           [,1]      [,2]       [,3]        [,4]
#[1,]  0.5757814 1.5117812 -0.6212406  1.12493092
#[2,] -0.3053884 0.3898432 -2.2146999 -0.04493361

#[[3]]
#            [,1]        [,2]        [,3] [,4]
#[1,] -0.01619026  0.91897737  0.61982575    0
#[2,]  0.94383621  0.78213630 -0.05612874    0
#[3,]  0.82122120  0.07456498 -0.15579551    0
#[4,]  0.59390132 -1.98935170 -1.47075238    0

Upvotes: 2

Related Questions