How to generate repeatable random multiple vectors with Specific Number of Values and Fixed Sum?
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Rik
2022-9-15
编辑:Rik
2022-9-15
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
更多回答(1 个)
Bruno Luong
2022-9-15
编辑:Bruno Luong
2022-9-15
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 个评论
Bruno Luong
2022-9-15
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)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!