How to create a matrix with random values in R?


Generally, a matrix is created with given values but if we want to create the matrix with random values then we will use the usual method with the matrix function. Random selection in R can be done in many ways depending on our objective, for example, if we want to randomly select values from normal distribution then rnorm function will be used and to store it in a matrix, we will pass it inside matrix function.

Example

 Live Demo

M1<-matrix(rnorm(36),nrow=6)
M1

Output

      [,1]       [,2]       [,3]       [,4]       [,5]       [,6]
[1,] 0.6379473 0.80467462 0.3398509 -0.4758089 1.292599611 1.24523919
[2,] 0.6832479 0.72504723 0.1156296 1.0744991 -1.104026056 -0.06716353
[3,] -0.3335057 0.97958477 1.0170069 -0.6235373 1.753112623 -1.00673575
[4,] 1.2129095 0.02634779 1.8586486 1.7806879 0.004136129 -1.07514918
[5,] -0.3875573 0.03151228 0.9427233 -0.3176984 1.094996191 -0.38860379
[6,] -0.4515077 -1.20904118 -0.7977128 0.8835648 0.762056845 1.17373715

Example

 Live Demo

M2<-matrix(runif(36),nrow=6)
M2

Output

         [,1]       [,2]       [,3]    [,4]       [,5]    [,6]
[1,] 0.4396258 0.01739362 0.4757882 0.6158219 0.01676052 0.9088362
[2,] 0.6166727 0.42229846 0.7145908 0.6728221 0.45173776 0.3929604
[3,] 0.2847230 0.18784287 0.2837930 0.7159734 0.47905932 0.8931971
[4,] 0.9507952 0.67257546 0.8142641 0.9384804 0.15925309 0.2147781
[5,] 0.8064644 0.38956299 0.9106267 0.9887673 0.47287897 0.6783567
[6,] 0.2491736 0.15939018 0.4609571 0.8415587 0.40739792 0.6961309

Example

 Live Demo

M3<-matrix(rexp(36),nrow=6)
M3

Output

      [,1]          [,2]        [,3]    [,4]       [,5]    [,6]
[1,] 1.22202307 3.99991458 0.26236165 0.5986415 0.96159749 0.6132856
[2,] 0.49396273 1.81352838 0.78716769 0.5748733 0.75516992 1.8929672
[3,] 0.68045397 0.12632613 1.41299054 0.6019353 0.20209984 0.2298947
[4,] 1.06590096 0.97355601 1.04021884 0.3013939 0.06098269 2.0683307
[5,] 0.05269628 0.34586402 0.09467971 2.4993345 1.92620852 0.3513072
[6,] 0.67667707 0.07113277 2.72820562 0.5355704 3.04340352 0.1567816

Example

 Live Demo

M4<-matrix(rpois(36,5),nrow=6)
M4

Output

   [,1]  [,2] [,3] [,4] [,5] [,6]
[1,] 5    6    6    3    7    4
[2,] 6    9    7    8    8    4
[3,] 3    7    6    5    7    8
[4,] 5    4    5    5    6    3
[5,] 4    8    8    3    2    3
[6,] 8    6    4    6    8    8

Example

 Live Demo

M5<-matrix(rpois(100,10),ncol=10)
M5

Output

   [,1]  [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 9    11   17   16   6    3    9    13   12   11
[2,] 11   11   10   5    10   9    7    8    10    8
[3,] 9    6    4    6    11   10   13   15    19   9
[4,] 6    9    13   15   16   7    7    13    9    9
[5,] 9    12   9    12   11   11   13   6     4    9
[6,] 13   4    13   15   1    10   9    10    12   7
[7,] 12   9    13   13   11   12   10   12    11   9
[8,] 10   7    10   15   10   8    17    9    7    9
[9,] 12   12   10   14   5    9    11    4    11   5
[10,] 16  8    7    9    14   10   11    9    8    14

Example

 Live Demo

M6<-matrix(rbinom(100,20,0.6),ncol=10)
M6

Output

    [,1]  [,2]  [,3] [,4]  [,5] [,6]  [,7]  [,8]  [,9] [,10]
[1,] 16    10    9    11    10    12    16    12   10    10
[2,] 14    12    15    10   10    16    12    7    15    12
[3,] 15    11    12    16   10    11    12    10    8    14
[4,] 12    13    11    12   10    11    15    12    12   13
[5,] 12    11    11    14   11    12    13    11    13   14
[6,] 12    12    10    12   17    10    15    12    13   12
[7,] 12    14    16    10   12    10    11    12    8    12
[8,] 14    14    13    8    15    11    8    13    13    15
[9,] 12    14    16    14   8    15     13    9    11    16
[10,] 9    15    7    10    12    16    13    14    12   11

Example

 Live Demo

M7<-matrix(runif(64,10,15),ncol=8)
M7

Output

      [,1]       [,2]    [,3]      [,4]    [,5]    [,6]       [,7]    [,8]
[1,] 10.76525 12.00224 12.29660 13.18522 13.49011 10.68609 14.71455 13.71585
[2,] 12.69970 10.76252 11.28360 13.80618 14.38140 12.32404 10.13467 13.46201
[3,] 13.69108 10.51077 12.99480 10.11390 12.08921 10.01959 12.23036 10.96521
[4,] 10.99119 12.42010 10.48445 14.96720 12.88057 11.30026 12.78480 11.24625
[5,] 13.39850 10.17959 13.94115 10.05765 11.17439 10.14223 11.75271 14.92220
[6,] 10.58127 10.12117 10.84108 10.88573 13.60804 12.54398 12.23277 10.32932
[7,] 11.20005 12.51919 12.19456 12.66209 10.62735 14.91281 11.95937 13.42508
[8,] 11.85078 13.30276 10.25665 12.85449 11.91376 10.89669 11.69131 12.37313

Example

 Live Demo

M8<-matrix(sample(1:20,100,replace=TRUE),ncol=10)
M8

Output

   [,1]  [,2] [,3] [,4] [,5]  [,6] [,7] [,8]  [,9] [,10]
[1,] 8    16   8    20    5    11    7    11    4    2
[2,] 6    14   20    8    18    3    7    19    3   18
[3,] 13   6    3    20    16    20    2    7    7   11
[4,] 12   20   4    20    18    1    18    10   4    1
[5,] 6    20   20   15    13    10    9    13   7    3
[6,] 4   14    15   11    13    14    20   5    16   19
[7,] 3   14    10   19    15    8    9     2    15   13
[8,] 4   19    8    19    4    18    14    11    5   12
[9,] 10   3    13   3    14    14    5      1    13   10
[10,] 18  8    5    7    15    18    16     6    14    3

Example

 Live Demo

M9<-matrix(sample(50:60,36,replace=TRUE),ncol=6)
M9

Output

   [,1] [,2] [,3] [,4]  [,5]  [,6]
[1,] 56 54    51    55    57    59
[2,] 54 50    56    54    56    57
[3,] 50 60    50    56    51    54
[4,] 53 55    50    55    60    57
[5,] 53 58    51    55    59    59
[6,] 58 52    50    56    57    56

Example

 Live Demo

M10<-matrix(sample(1000:2000,64,replace=TRUE),ncol=8)
M10

Output

    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 1497 1437 1338 1272 1235 1339 1422 1923
[2,] 1416 1008 1843 1804 1678 1314 1355 1258
[3,] 1440 1525 1592 1673 1207 1295 1723 1028
[4,] 1863 1523 1033 1060 1388 1321 1724 1464
[5,] 1852 1874 1630 1490 1858 1454 1844 1366
[6,] 1188 1304 1712 1445 1037 1390 1617 1712
[7,] 1904 1842 1545 1859 1578 1023 1298 1131
[8,] 1154 1577 1716 1005 1350 1695 1542 1243

Updated on: 08-Sep-2020

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements