Learn R Programming

NMF (version 0.5.06)

rmatrix: Generates a Random Matrix Using Any Given Distribution Function

Description

This function provides a short-cut to generate a random matrix whose entries are drawn any given random distribution, as soon as this one is implemented as an Rfunction similar to: runif, rnorm, etc ...

It basically wraps the following call:

matrix(dist(nrow*ncol, ...), nrow, ncol)

in a quicker and intuitive call:

rmatrix(nrow, ncol, dist, ...)

Usage

rmatrix(nrow, ncol, dist = runif, byrow = FALSE, dimnames = NULL, ...)

Arguments

nrow
the desired number of rows of the generated matrix.
ncol
the desired number of columns of the generated matrix.
dist
the distribution function from which to draw the matrix entries. It must be a -- distribution -- function whose first parameter is the number of draws n to be done, and return a vector of length n.
byrow
logical. If FALSE (the default) the matrix is filled by columns, otherwise the matrix is filled by rows. See matrix.
dimnames
A dimnames attribute for the matrix: NULL or a list of length 2 giving the row and column names respectively. An empty list is treated as NULL, and a list of length one as
...
any extra parameters to pass to function dist.

Value

  • a matrix of dimension nrow x ncol whose entries are drawn from distribution dist

See Also

runif, rnorm or any other rABCDE similar distribution functions.

Examples

Run this code
##---- Should be DIRECTLY executable !! ----
##-- ==>  Define data, use random,
##--	or do  help(data=index)  for the standard data sets.

## The function is currently defined as
function(nrow, ncol, dist=runif, ...){
	# check that 'dist' is a function.
	if( !is.function(dist) )
		stop("NMF::rmatrix - invalid value for argument 'dist': must be a function [class(dist)='", class(dist), "'].")
	
	# build the random matrix using the distribution function
	matrix(dist(nrow*ncol, ...), nrow, ncol)
  }

Run the code above in your browser using DataLab