How to generate repeatable random multiple vectors with Specific Number of Values and Fixed Sum?
Ältere Kommentare anzeigen
Hello everybody,
I want to create repeatable random multiple vectors with Specific Number of Values and Fixed Sum.
for example, in case of Specific Number of Values 3 and Fixed Sum 10,
If m row is 5, the random number can be [1; 2; 2; 4; 1], 1+2+2+4+1 =10 and Number of Values 3 (1,2 and 4 used)
or [1.1; 2.1; 2.1; 3.6; 1.1]. 1.1+2.1+2.1+3.6+1.1 =10 and Number of Values 3 (1.1, 2.1 and 3.6 used)
in case of Specific Number of Values 2 and Fixed Sum 10,
If m row is 4, the random number can be [2; 3; 3; 2], 2+3+3+2 =10 and Number of Values 2 (2 and 3 used)
or [1.5; 3.5; 3.5; 1.5]. 1.5+3.5+3.5+1.5 =10 and Number of Values 2 (1.5 and 3.5 used)
in case of Specific Number of Values 1 and Fixed Sum 10,
If m row is 4, the random number can be [2.5; 2.5; 2.5; 2.5]. 2.5+2.5+2.5+2.5 =10 and Number of Values 1 (2.5 only used)
Specific Number of Values 1 means only one value can be used,
I found the randfixedsum code provided by Roger Stafford. It is wonderful code, but it generate all random numers
without Specific Number of Values.
Please give me some help how to make the code for it.
Akzeptierte Antwort
Weitere Antworten (1)
Bruno Luong
am 15 Sep. 2022
Bearbeitet: Bruno Luong
am 15 Sep. 2022
Just split randomly at most m times on top of randfixedsum output
m=5;
nvalues=3;
starget=10;
n=diff([0 sort(randperm(m-1,nvalues-1)) m]); % nvalues integers sum to m
r=randfixedsum(nvalues,1,starget,0,10); % nvalues positive numbers sum to starget
r=repelem(r./n',n)
2 Kommentare
Smithy
am 15 Sep. 2022
This code generate a slightly different statistics property. The difference between vallues has probably smaller rms than my forst code, if such difference is important for whatever simulation you do.
m=5;
nvalues=3;
starget=10;
i = sort(randperm(m-1,nvalues-1));
n = diff([0 i m]); % nvalues integers sum to m
r = randfixedsum(m,1,starget,0,10)'; % m positive numbers sum to starget
c = cumsum(r);
r = diff([0 c(i) starget]); % nvalues integers sum to starget
r = repelem(r./n, n)
Kategorien
Mehr zu Univariate Discrete Distributions finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!