Hendrik
Hendrik

Reputation: 175

R repmat function for sparse matrices

for my r project, I need to repeat several big (i.e. bigger than 1000x1000) matrices. I found two versions the matlab repmat-function in r, that both work, but have severe limitations, so that I am unable to use them. Does anyone have another approach to solve this problem?


To decrease memory usage, I use the sparse-functions from the Matrix-Package (Diagonal(), Matrix(..., sparse=TRUE)).

> m <- Diagonal(10000)
> object.size(m)
1168 bytes

Now, to repeat this matrix I use a r translation of the matlab function repmat (which can be found here):

repmat <- function(X, m, n){
    mx <- dim(X)[1]
    nx <- dim(X)[2]
    return(matrix(t(matrix(X,mx,nx*n)),mx*m,nx*n,byrow=T))
}

Unfortunately, this method uses the standard/dense version of a matrix and only works up to a certain object size, which is exceeded pretty fast within my project. Simply swapping the matrix(...) function with a Matrix(..., sparse=TRUE) one also wont work, because of the different parameter definitions for the matrix dimensions.

The only other solution would be the repmat-version from the pcaMethods-Package, where I am able to use the sparse matrices:

repmat <- function(mat, M, N) {
    ## Check if all input parameters are correct
    if( !all(M > 0, N > 0) ) {
        stop("M and N must be > 0")
    }    

    ## Convert array to matrix
    ma <- mat
    if(!is.matrix(mat)) {
        ma <- Matrix(mat, nrow=1, sparse=TRUE)
    }

    rows <- nrow(ma)
    cols <- ncol(ma)
    replicate <- Matrix(0, rows * M, cols * N, sparse=TRUE)

    for (i in 1:M) {
        for(j in 1:N) {
            start_row <- (i - 1) * rows + 1
            end_row <- i * rows
            start_col <- (j - 1) * cols + 1
            end_col <- j * cols
            replicate[start_row:end_row, start_col:end_col] <- ma
        }
    }

     return(replicate)
}

However, this functions does the job, but needs a lot of runtime (probably because of the nested loops). My only option left is to increase the overall memory.limit, but this only results in running out of physical memory.


I am at my wits end here. Any help or advice would be appreciated. Thank you in advance for your replies.

Upvotes: 2

Views: 604

Answers (1)

Roland
Roland

Reputation: 132999

Use the Matrix methods for rbind and cbind:

repMat <- function(X, m, n){
  Y <- do.call(rbind, rep(list(X), m))
  do.call(cbind, rep(list(Y), n))
}


system.time(res <- repMat(m, 20, 30))
#user  system elapsed 
#0.48    0.44    0.92
str(res)
#Formal class 'dgCMatrix' [package "Matrix"] with 6 slots
#  ..@ i       : int [1:6000000] 0 10000 20000 30000 40000 50000 60000 70000 80000 90000 ...
#  ..@ p       : int [1:300001] 0 20 40 60 80 100 120 140 160 180 ...
#  ..@ Dim     : int [1:2] 200000 300000
#  ..@ Dimnames:List of 2
#  .. ..$ : NULL
#  .. ..$ : NULL
#  ..@ x       : num [1:6000000] 1 1 1 1 1 1 1 1 1 1 ...
#  ..@ factors : list()

object.size(res)
#73201504 bytes

Upvotes: 3

Related Questions