Generating dispersed (non-integer) random matrix/array that sums to a particular value
Show older comments
One of the most suggested (in fact the only one to my finding) for generating random numbers (<1) that will sum to 1 is Random Vectors with Fixed Sum by Roger Stafford. However, what I noticed is that the data generated is not well dispersed. e.g.,
P = randfixedsum(10,10000,1,0.05,0.9); % a 10-by-100000 matrix where each column of P sums to 1 and each elements is between 0.05 and 0.9
find(any(P>0.5))
ans =
1×0 empty double row vector
So far, every single time I tried it results in an empty vector - it always limits itself within below 0.5. Is there a way I could generate more dispersed data where it would include values between 0.05 and 0.9 (for the above example)?
Thanks in advance for your kind help.
FYI: I have tried this (took help from one of the MATLAB answers)
function P = rand_fixed_sum_2(p,n) % p number of columns, and n number of rows and each column sums to 1
for j = 1:p
n1=10^(n-1);
m=1:n1;
a=m(sort(randperm(n1,n)));
b=diff(a);
b(end+1)=n1-sum(b);
P(:,j) = (b/sum(b))';
end
end
But obviously the value of n1 is not feasible for higher dimensions (n>5). However, for lower dimensions, by tweaking n1, I could get much more dispersed data.
Accepted Answer
More Answers (0)
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!