简体   繁体   中英

Creat a uniformly distributed random variable by Matlab

I want to create a random variable that takes a value in set {-2, -1, 1, 2} with probability p = 0.25. How can I do it in Matlab ?

Using randi . If you just want a single value:

>> x = [-2, -1, 1, 2];
>> random_var = x(randi(length(x)))
random_var =
     2

If you want more than one:

>> random_vars = x(randi(length(x), 3, 10))
random_vars =
     2    -1     2     2    -2     2    -2     1    -1     1
     1     1    -2    -1    -1     2     2     2     1    -2
    -2     2     2     2     2     1     2     1    -2    -1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM