rsparsematrix {Matrix} | R Documentation |
Generate a Random Sparse Matrix Efficiently.
rsparsematrix(nrow, ncol, density, nnz = round(density * maxE), symmetric = FALSE, rand.x = function(n) signif(rnorm(nnz), 2), ...)
nrow, ncol |
number of rows and columns, i.e., the matrix
dimension ( |
density |
optional number in [0,1], the density is the
proportion of non-zero entries among all matrix entries. If
specified it determines the default for |
nnz |
number of non-zero entries, for a sparse matrix typically
considerably smaller than |
symmetric |
logical indicating if result should be a matrix of
class |
rand.x |
the random number generator for the |
... |
optionally further arguments passed to
|
The algorithm first samples “encoded” (i,j)s without
replacement, via one dimensional indices, if not symmetric
sample.int(nrow*ncol, nnz)
, then gets
x <- rand.x(nnz)
and calls
sparseMatrix(i=i, j=j, x=x, ..)
.
a sparseMatrix
, say M
of dimension (nrow,
ncol), i.e., with dim(M) == c(nrow, ncol)
, if symmetric
is not true, with nzM <- nnzero(M)
fulfilling
nzM <= nnz
and typically, nzM == nnz
.
Martin Maechler
set.seed(17)# to be reproducible M <- rsparsematrix(8, 12, nnz = 30) # small example, not very sparse M M1 <- rsparsematrix(1000, 20, nnz = 123, rand.x = runif) summary(M1) ## a random *symmetric* Matrix (S9 <- rsparsematrix(9, 9, nnz = 10, symmetric=TRUE)) # dsCMatrix nnzero(S9)# ~ 20: as 'nnz' only counts one "triangle" ## a [T]riplet representation sparseMatrix: T2 <- rsparsematrix(40, 12, nnz = 99, giveCsparse=FALSE) head(T2)