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 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 11 月 30 日
v=randi(4,1,5)
a=20-sum(v)
x=zeros(5,ceil(a/5));
x(1:a)=1
v=v+sum(x',1)
Vivek
Vivek 2013 年 12 月 2 日
Ya Azzi. Working fine. Thank you.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSparse Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by