How do I distribute probability randomly over n values in matlab? If I have 128 vectors. I want to assign a random probabilty to all of them such that the sum of all of them equals to 1.
e.g. n=4 p1=0.37 p2=0.21 p3=0 p4=0.42
How do I distribute probability randomly over n values in matlab? If I have 128 vectors. I want to assign a random probabilty to all of them such that the sum of all of them equals to 1.
e.g. n=4 p1=0.37 p2=0.21 p3=0 p4=0.42
Depending on how random you need to be, Roger Stafford takes a more stringent approach.
You can just divide the vector by the sum of it's elements. For example, for a vector of length 4 you can do:
>> v = rand(4, 1);
>> v = v/sum(v)
v =
0.2951
0.3281
0.0460
0.3308
>> sum(v)
ans =
1.0000
Note, I am assuming you want uniformly distributed numbers, since you don't state what distribution you want in the question.