How to create a random row vector whose sum of elements and number of elements is at my desire?
11 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Vivek
el 30 de Nov. de 2013
Comentada: Andrei Bobrov
el 3 de Dic. de 2013
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?
2 comentarios
Respuesta aceptada
Andrei Bobrov
el 30 de Nov. de 2013
n = 5;
m = 20;
a = rand(n,1);
out = round(a/sum(a)*m);
out(1) = out(1) - sum(out) + m;
3 comentarios
Roger Stafford
el 2 de Dic. de 2013
Editada: Roger Stafford
el 2 de Dic. de 2013
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
el 3 de Dic. de 2013
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.
Más respuestas (2)
Roger Stafford
el 30 de Nov. de 2013
Editada: Roger Stafford
el 30 de Nov. de 2013
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 comentarios
Roger Stafford
el 1 de Dic. de 2013
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
el 30 de Nov. de 2013
Editada: Azzi Abdelmalek
el 30 de Nov. de 2013
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 comentarios
Azzi Abdelmalek
el 30 de Nov. de 2013
v=randi(4,1,5)
a=20-sum(v)
x=zeros(5,ceil(a/5));
x(1:a)=1
v=v+sum(x',1)
Ver también
Categorías
Más información sobre Sparse Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!