How to generate multiple Vectors with random variables but sum 1
Afficher commentaires plus anciens
Hello Folks, i want to create multiple Vectors with random variables (btw. 0 and 1) but sum 1
for example [0 1 0 0] or [0.7 0.2 0 0.1] and so on
i want to scatterplot a large number of random portfolios and therefore i need random weights.
i'm sure there is an easy way but since i'm new to matlab and after looking for a long time i figured i ask the community.
thanks for your help.
Réponse acceptée
Plus de réponses (1)
Sean de Wolski
le 20 Jan 2012
Easiest way is to normalize by the sum:
A = rand(1,10);
B=A./sum(A);
sum(B)
However, there are some things that make this non-truly random. If you care:
3 commentaires
the cyclist
le 20 Jan 2012
I highly recommend using Roger Stafford's FEX contribution, and not taking the "easy way out".
Sean de Wolski
le 20 Jan 2012
It really depends on the use case. For a homework problem, I would take the easy way out.
Matthias
le 1 Fév 2012
Catégories
En savoir plus sur Random Number Generation dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!