r_matrix | R Documentation |
Generate a random sparse matrix, just like Matrix::rsparsematrix
or (matrix(runif(nrow * ncol), nrow,))
, but much faster.
Generation of transpose-identical matrices is also supported without additional computational cost.
r_matrix(nrow, ncol, transpose_identical = FALSE)
r_sparsematrix(
nrow,
ncol,
inv_density,
transpose_identical = FALSE,
pattern = FALSE
)
nrow |
number of rows |
ncol |
number of columns |
transpose_identical |
should the matrix be transpose-identical? |
inv_density |
an integer giving the inverse density of the matrix (i.e. 10 percent density corresponds to |
pattern |
should a pattern matrix ( |
r_unif
r_matrix
, r_unif
, r_binom
# generate a simple random matrix
A <- r_matrix(10, 10)
# generate two matrices that are transpose identical
set.seed(123)
A1 <- r_matrix(10, 100, transpose_identical = TRUE)
set.seed(123)
A2 <- r_matrix(100, 10, transpose_identical = TRUE)
all.equal(t(A2), A1)
# generate a transpose-identical pair of speckled matrices
set.seed(123)
A <- r_sparsematrix(10, 100, inv_density = 10, transpose_identical = TRUE)
set.seed(123)
A <- r_sparsematrix(100, 10, inv_density = 10, transpose_identical = TRUE)
all.equal(t(A), A)
Matrix::isSymmetric(A[1:10, 1:10])
heatmap(as.matrix(A), scale = "none", Rowv = NA, Colv = NA)
# note that density is probabilistic, not absolute
A <- replicate(1000, r_sparsematrix(100, 100, 10))
densities <- sapply(A, function(x) length(x@i) / prod(dim(x)))
plot(density(densities)) # normal distribution centered at 0.100
range(densities)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.