Random positive numbers with constant sum
Show older comments
How to form a matrix such that every time the numbers add up to be the same
z=0.4
minimum z= 0.2
maximum z= 0.6
R is a random number between 0.2 and 0.6
a 1x10 R matrix is generated but the sum must be equal to (0.4 x 10 = 4)
I tried running the following using randn but it gives negatives numbers and the sums are not constant
z=0.4
zmax=1.5*z;
zmin=0.5*z;
R=zmin+randn(1,10)*(zmax-zmin);
Eg. Desired random number R matrix
[0.35 0.27 0.43 0.51 0.54 0.44 0.37 0.29 0.59 0.21]
sum(R)=4 < must be consistent for every matrix generated
Accepted Answer
More Answers (1)
Torsten
on 8 Nov 2017
1 vote
https://de.mathworks.com/matlabcentral/fileexchange/9700-random-vectors-with-fixed-sum
Best wishes
Torsten.
Categories
Find more on Creating and Concatenating Matrices 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!