Skip to contents

Create a matrix of given dimension and fill it with random integer values

Usage

randomMatrix(dim = c(sample(10, 1), sample(10, 1)), values = seq_len(100))

Arguments

dim

integer vector of length two containing the number of rows and columns, respectively, that the output matrix shall contain

values

set of values to be used within the matrix

Examples


# By default, the matrix has a random number of rows between 1 and 10 and
# a random number of columns between 1 and 10 and random values of 1:100
randomMatrix()
#>       [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#>  [1,]   85   11   74   56   55   79    7
#>  [2,]  100   42   67   52   73   63   51
#>  [3,]   65   40  100   57   89   23   92
#>  [4,]    3   15   14   17   10   57   12
#>  [5,]    7   82   66   87   73   31    3
#>  [6,]   49   44   69   31   83   59   16
#>  [7,]   70   54   82   93   89   75   72
#>  [8,]   91   85   37   52   51   54   12
#>  [9,]   12   58    4   74   29   40   66
#> [10,]   39   29   83   24   49   60   71

# You may specify the dimensions (here: 5 rows, 3 columns)...
randomMatrix(dim = c(5, 3))
#>      [,1] [,2] [,3]
#> [1,]   82    2   16
#> [2,]   26   73   87
#> [3,]   64   57   15
#> [4,]   33   43   32
#> [5,]   90   33   84

# ... and the set of values to be used within the matrix
randomMatrix(dim = c(5, 3), values = c(0, 0.5, 1, NA))
#>      [,1] [,2] [,3]
#> [1,]    1  0.0  0.5
#> [2,]    0  0.5  1.0
#> [3,]   NA  1.0   NA
#> [4,]    0  1.0  0.5
#> [5,]    0   NA  1.0