# two sparse matrices with row names
X <- rSparseMatrix(1e4, 1e3, 1e4)
Y <- rSparseMatrix(1e4, 1e3, 1e4)
rownames(X) <- 1:nrow(X)
rownames(Y) <- 1:nrow(Y)
# the basic KhatriRao product from the Matrix package is very fast
# but almost all rows are empty
system.time(M <- KhatriRao(X, Y))
sum(rowSums(M)==0)/nrow(M) # 99.9% empty rows
# To produce all row names takes a long time with KhatriRao from Matrix
# with the current example with 1e8 row names it took a minute on my laptop
# so: don't try the following, except on a large machine!
# \donttest{
system.time(M <- KhatriRao(X, Y, make.dimnames = TRUE))
# }
# Using the current special version works just fine and is reasonably quick
system.time(M <- rKhatriRao(X, Y))
Run the code above in your browser using DataLab