The problem with elementary division is that if your matrices are sparse, then as a result you will get a lot of Inf and NaN , and this will make it dense. This is why you get errors from memory.
If you want to replace Inf and NaN zeros as a result, then the solution will be relatively simple, you just get summary() both matrices and work directly with indexes and values.
You will need to limit the index indices A and B their intersection and divide by this. To get the intersection of index pairs, you can use merge() .
Here is a quick and dirty implementation:
# Some example data A <- sparseMatrix(i=c(1,1,2,3), j=c(1,3,1,3), x=c(1,1,2,3)) B <- sparseMatrix(i=c(3,2,1), j=c(3,2,1), x=c(3,2,1)) sdiv <- function(X, Y, names=dimnames(X)) { sX <- summary(X) sY <- summary(Y) sRes <- merge(sX, sY, by=c("i", "j")) sparseMatrix(i=sRes[,1], j=sRes[,2], x=sRes[,3]/sRes[,4], dimnames=names) } sdiv(A, B)
Thanks to flodel for suggesting using summary and merge .