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?

 Accepted Answer

Use randfixedsum.m function from the following link:
and write
randfixedsum(5,1,1,0,1)

4 Comments

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.

Sign in to comment.

More Answers (0)

Categories

Find more on Random Number Generation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!