random numbers that sum up to a definite integer

hi, i would like to generate a column vector using rand function e.g rand(5,1), so that none of the element is bigger than 1 and less than 0. and i want the sum of all the elements to equal 1 exactly. how do i do it?

 Réponse acceptée

Birdman
Birdman le 21 Fév 2018
Use randfixedsum.m function from the following link:
and write
randfixedsum(5,1,1,0,1)

4 commentaires

but it is giving me a warning: Function power has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.
and if i write a code like this-
x=randfixedsum(5,1,1,0,1)
then an error message is shown: Output argument "x" (and maybe others) not assigned during call to "randfixedsum".
Randfixedsum has no function defined named power, nor, knowing Roger, would he ever have done that.
Are you sure that you did not define a function named power yourself? Try this:
which power -all
That would indeed cause a major problem in MATLAB, had you done that.
thnx, got the solution.

Connectez-vous pour commenter.

Plus de réponses (0)

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!

Translated by