
Matrix of random values, possibly with standardization by row or column
Source:R/runifMatrix.r
runifMatrix.RdThis 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.
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.
Examples
rows <- 4
cols <- 3
runifMatrix(rows, cols)
#> [,1] [,2] [,3]
#> [1,] 0.1366097 0.2089333 0.8653553
#> [2,] 0.6393154 0.4044185 0.8841806
#> [3,] 0.7250252 0.7936403 0.9221945
#> [4,] 0.9543911 0.4681930 0.6509773
standByRows <- runifMatrix(rows, cols, stand = 'r')
standByRows
#> [,1] [,2] [,3]
#> [1,] 0.3752459 0.43006231 0.1946918
#> [2,] 0.4848543 0.05579771 0.4593480
#> [3,] 0.1670546 0.41151256 0.4214328
#> [4,] 0.1249850 0.30350841 0.5715065
rowSums(standByRows)
#> [1] 1 1 1 1
standByCols <- runifMatrix(rows, cols, stand = 'c')
standByCols
#> [,1] [,2] [,3]
#> [1,] 0.19774914 0.06025738 0.002225804
#> [2,] 0.03320843 0.29285847 0.317199243
#> [3,] 0.44284582 0.17505172 0.275401726
#> [4,] 0.32619661 0.47183243 0.405173227
colSums(standByCols)
#> [1] 1 1 1
standByMe <- runifMatrix(rows, cols, stand = 'm')
standByMe
#> [,1] [,2] [,3]
#> [1,] 0.10509609 0.02610047 0.13796654
#> [2,] 0.13877849 0.13007765 0.06135501
#> [3,] 0.04684221 0.01757568 0.04080284
#> [4,] 0.12489690 0.04429580 0.12621233
sum(standByMe) # whenever you're in trouble
#> [1] 1