1

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

Amro
  • 123,847
  • 25
  • 243
  • 454

2 Answers2

2

Depending on how random you need to be, Roger Stafford takes a more stringent approach.

Chuck Sweet
  • 123
  • 5
1

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.

Chris
  • 44,602
  • 16
  • 137
  • 156
  • Can you prove the result is truly uniform? That is the result covers with true distribution all the cases of four numbers that sum up to 1? – Bogdan Lataianu Feb 21 '13 at 17:03
  • In fact this is false-see deeves'link in this question http://stackoverflow.com/questions/2171074/generating-a-probability-distribution? – Bogdan Lataianu Feb 21 '13 at 17:20