Skip to contents

This function creates a matrix populated by random uniform values and (at the user's discretion), standardizes the rows, columns, or entire matrix so values sum to 1.

Usage

runifMatrix(nrow, ncol, min = 0, max = 1, stand = NULL)

Arguments

nrow, ncol

Number of rows and columns.

min, max

Minimum and maximum value of values.

stand

Any of:

  • NULL (default): No standardization.

  • 'rows': Standardize so rows sum to 1.

  • 'columns': Standardize so columns sum to 1.

  • 'matrix': Standardize so all values sum to 1.

Partial matching is used and case is ignored.

Value

A numeric matrix.

Examples


rows <- 4
cols <- 3

runifMatrix(rows, cols)
#>            [,1]      [,2]      [,3]
#> [1,] 0.69658366 0.9373302 0.5485914
#> [2,] 0.40572872 0.2163802 0.8275014
#> [3,] 0.06563664 0.6660920 0.1275577
#> [4,] 0.12649262 0.2039007 0.2611861

standByRows <- runifMatrix(rows, cols, stand = 'r')
standByRows
#>            [,1]      [,2]       [,3]
#> [1,] 0.20598150 0.5363586 0.25765990
#> [2,] 0.01633418 0.7952124 0.18845340
#> [3,] 0.39667127 0.2394529 0.36387580
#> [4,] 0.29162241 0.6865973 0.02178034
rowSums(standByRows)
#> [1] 1 1 1 1

standByCols <- runifMatrix(rows, cols, stand = 'c')
standByCols
#>            [,1]       [,2]       [,3]
#> [1,] 0.44582697 0.35215753 0.27710172
#> [2,] 0.09408981 0.41186653 0.36968156
#> [3,] 0.01621549 0.16156558 0.01683465
#> [4,] 0.44386773 0.07441036 0.33638207
colSums(standByCols)
#> [1] 1 1 1

standByMe <- runifMatrix(rows, cols, stand = 'm')
standByMe
#>            [,1]       [,2]       [,3]
#> [1,] 0.03718979 0.11089965 0.12052464
#> [2,] 0.00276465 0.05654992 0.15991340
#> [3,] 0.02221696 0.06694011 0.07929838
#> [4,] 0.11852199 0.12222543 0.10295508
sum(standByMe) # whenever you're in trouble
#> [1] 1