How to fill matrix with random numbers in R?

后端 未结 6 1484
自闭症患者
自闭症患者 2021-02-03 20:18
expand.grid(i=rexp(5,rate=0.1))

It creates just one col but is there some way to multiply this easily to 5 cols? I mean the matlab-way-of-doing-things

相关标签:
6条回答
  • 2021-02-03 20:42

    Here's a simple solution:

    replicate(rexp(10),20)
    
    0 讨论(0)
  • 2021-02-03 20:43

    you can do something like:

    matrix(rexp(200), 10)
    

    And of course use what ever distribution you want.

    0 讨论(0)
  • 2021-02-03 20:45

    ?matrix will tell you lots! and rexp is the function to generate a random exponential distribution.

    mat <- matrix(data = rexp(200, rate = 10), nrow = 10, ncol = 20)
    
    0 讨论(0)
  • 2021-02-03 20:52

    Use the matrix function:

    matrix(rexp(200, rate=.1), ncol=20)
    

    ETA: If you want to do it without repeating the 200, you can define a function to do so:

    fill.matrix = function(expr, nrow=1, ncol=1) {
        matrix(eval(expr, envir=list(x=nrow*ncol)), nrow=nrow, ncol=ncol)
    }
    
    fill.matrix(rexp(x, rate=.1), nrow=10, ncol=20)
    

    The x thus becomes the dummy variable you're talking about. Is that what you're looking for?

    0 讨论(0)
  • 2021-02-03 20:55

    You can generate a uniformly distributed 10 by 15 random matrix in the following code line:

    matrix(runif(10*15), nrow = 10, ncol = 15)
    
    0 讨论(0)
  • 2021-02-03 20:56

    Use this:

    matris=matrix(rnorm(10),5,20)
    
    0 讨论(0)
提交回复
热议问题