문제

I want to generate a random vector such that the elements add up to 1. Is that possible in Octave?

I have a bunch of mutually exclusive events (say 100) and I want to assign random probabilities (non-zero and hopefully with a normal distribution) to them such that their probabilities sum up to 1. Is there a way to do that in Octave?

I know of rand(1,100) but I do not know if it is possible to make sure that the values sum up to a constant.

도움이 되었습니까?

해결책

Generate a random vector. It will sum up to S. Divide all elements by S - it now sums up to 1.

x = rand(1, 100)
x /= sum(x)
sum(x)
# ==> 1

EDIT: added code, renamed the confusing variable.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top