How do you generate a matrix with a predefined probability of occurrence for each possible value? For example, I need to generate a 3-by-5 matrix consisting of only 1
, 0
and -1
, where the probability of occurrence of 1
is 0.25
, the probability of occurrence of -1
is 0.25
and the probability of occurrence of 0
is 0.5
?
Asked
Active
Viewed 70 times
-1
1 Answers
0
The simple answer is:
round(rand(3,5)*2)-1
more generally you have to find a function that produces random variables according to your probability distribution. Then you can use reshape()
to get the right array.

Dennis Klopfer
- 759
- 4
- 17
-
got it.thank you all – rose Oct 26 '15 at 10:29