How to generate repeatable random multiple vectors with Specific Number of Values and Fixed Sum?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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.
0 commentaires
Réponse acceptée
Rik
le 15 Sep 2022
Modifié(e) : Rik
le 15 Sep 2022
You should try to restructure your problem to make it fit the tools you already have.
If we figure out a way to generate an order, we can use randfixedsum to generate the actual values.
If you have m=5,N=2,S=10, then you can first list all possible permutations:
m = 5;N = 2;S = 10;
rng(0) % fix random seed to make this example the same each time
A = rand;B = rand;
[A A A A B];
[A A A B B];
[A A B B B];
[A B B B B];
A and B must each occur at least once, so we only need to generate a list for the m-N remaining elements.
remaining_elements = randi(N,1,m-N)
Now we can count how often each element is selected:
Occurences_per_value = 1+histcounts(remaining_elements,0.5:(0.5+N))
Now let's actually generate some random numbers:
x = randfixedsum(N,1,S,0,S).'
You will notice, this only results in N numbers, not m. The trick is to divide these numbers and replicate them:
x./Occurences_per_value
We can use repelem to replicate the numbers as often as we need them:
repelem('AB',Occurences_per_value)
RandomData = repelem(x./Occurences_per_value,Occurences_per_value)
But we have a last problem: these values are not randomly ordered. Once last call to randperm will fix that:
RandomData = RandomData(randperm(end))
All in one compact (uncommented) block:
Occurences_per_value = 1+histcounts(randi(N,1,m-N),0.5:(0.5+N))
RandomData = repelem((randfixedsum(N,1,S,0,S).')./Occurences_per_value,Occurences_per_value)
RandomData = RandomData(randperm(end))
function [x,v] = randfixedsum(n,m,s,a,b)
% https://www.mathworks.com/matlabcentral/fileexchange/9700-random-vectors-with-fixed-sum
end
Plus de réponses (1)
Bruno Luong
le 15 Sep 2022
Modifié(e) : Bruno Luong
le 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 commentaires
Bruno Luong
le 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)
Voir également
Catégories
En savoir plus sur Get Started with Statistics and Machine Learning Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!