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