How to create a random row vector whose sum of elements and number of elements is at my desire?

7 次查看(过去 30 天)
For example I want a row vector(1,5) and its total sum should be 20. Is there any function which randomly returns a 1*5 vector with 20 as its sum of elements?

采纳的回答

Andrei Bobrov
Andrei Bobrov 2013-11-30
n = 5;
m = 20;
a = rand(n,1);
out = round(a/sum(a)*m);
out(1) = out(1) - sum(out) + m;
  3 个评论
Roger Stafford
Roger Stafford 2013-12-2
编辑:Roger Stafford 2013-12-2
I hope you won't mind my criticizing your solution, Andrei, but I have noticed that a small proportion of the time, this algorithm comes up with a -1 at out(1) in the case m = 20 and n = 5. Here is an example:
m = 20;
a = [0.03;0.23;0.22;0.56;0.65]; % <-- Suppose 'rand' gives this
out = round(a/sum(a)*m);
out(1) = out(1) - sum(out) + m;
out' = -1 3 3 7 8
For n = 6 it is even possible to come with -2 in that position:
m = 20;
a = [0.045;0.451;0.351;0.251;0.751;0.151]; % <-- or this
out = round(a/sum(a)*m);
out(1) = out(1) - sum(out) + m;
out' = -2 5 4 3 8 2
It appears to be caused by unlucky roundings in which the sum of fractional numbers is just m at one point but after rounding their sum is greater than m with the first element having rounded down to zero. Your adjustment
out(1)=out(1)-sum(out)+m
will then produce a negative value at out(1).
Andrei Bobrov
Andrei Bobrov 2013-12-3
Hi Roger! I fully agree with you, for the condition when all positive numbers in vector out , it is need to replace round at floor .
And your solution very nice.

请先登录,再进行评论。

更多回答(2 个)

Roger Stafford
Roger Stafford 2013-11-30
编辑:Roger Stafford 2013-11-30
If only positive integers are allowed (>0) then do:
m = 20; % Choose the desired sum
n = 5; % Choose the number of integers
v = diff([0,sort(randperm(m-1,n-1)),m]);
If zeros are permitted, then do:
m = 20; % Choose the desired sum
n = 5; % Choose the number of integers
v = diff([0,sort(randperm(m+n-1,n-1)),m+n])-ones(1,n);;
Note: These algorithms randomly select among all possible solutions with uniform probability.
Note 2: If you have the older version of 'randperm' that has only one input argument, you can substitute
p = randperm(n);
p = p(1:k);
for
p = randperm(n,k);
Note 3; There is no limitation such as n<=15 on the size of n in randperm(n). It is very different from perm.
  3 个评论
Roger Stafford
Roger Stafford 2013-12-1
I neglected to point out, Vivek, that the total number of possible solutions to your problem is implicit in these two expressions. If zeros are not permitted, the total is (m-1)!/(n-1)!/(m-n)!, namely the number of possible combinations of n-1 things chosen out of m-1 things. With zeros permitted it is (m+n-1)!/(n-1)!/m!, the number of combinations of n-1 things out of m+n-1 things. The probabilities are equal for all possibilities simply because the combinations produced by doing the two-argument 'randperm' followed by a 'sort' should all be of equal probability (provided Mathworks' random number generator is working properly.)

请先登录,再进行评论。


Azzi Abdelmalek
Azzi Abdelmalek 2013-11-30
编辑:Azzi Abdelmalek 2013-11-30
Edit
n=20;
m=21;
p=fix(m/n);
v=randi(p,1,n);
a=m-sum(v);
x=zeros(n,ceil(a/n));
x(1:a)=1;
v=v+sum(x',1)
  3 个评论

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Sparse Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by