How to generate matrix with mean equal to a specific number?
3 views (last 30 days)
Show older comments
How can I generate a random matrix A with
(for example A is 10x10) mean(A(:)) = 0.5 or says sum(sum(A)) / (10*10) = 0.5
Every element in matrix randomly from 0 to 1.
Thanks
Kai
2 Comments
Thorsten
on 10 Jan 2013
Edited: Thorsten
on 10 Jan 2013
It is not possible to meet your conditions: If every element must be picked randomly between 0 and 1, it can be easily > 0.5, invalidating your condition that the sum must be equal to 0.5. Likewise, if the sum must be 0.5, the first element r(1) can be picked from [0, 0.5], the second element r(2) from [0, 0.5-sum(r)], and so on, until the final element which is not random at all but must be 0.5-sum(r), all invalidating your condition that the number must be randomly between 0 and 1. So what do you actually want to do?
Roger Stafford
on 10 Jan 2013
What you are saying, Thorsten, would only be true if one assumes that all number selections must be statistically mutually independent of one another, but Kai stated no such requirement. In the example I gave here of choosing three random variables, each between 0 and 1, whose sum is 3/2, the random variables can be so selected that the planar two-dimensional hexagon of number triples which satisfy all these conditions is uniformly distributed area-wise. It is my opinion that this satisfies Kai's requirements for randomness, but obviously the three random variables would not be mutually independent.
Accepted Answer
Roger Stafford
on 10 Jan 2013
Edited: Roger Stafford
on 10 Jan 2013
You are asking for the sum of n = 100 random numbers with values between 0 and 1 to have a sum of exactly 50. That is just what the File Exchange routine randfixedsum is designed to accomplish.
Also with multiple solutions it will produce a statistically uniform distribution on the (n-1)-dimensional polytope which satisfies this sum condition. For example, if your n were equal to 3, it would produce a uniform distribution within the two-dimensional hexagon x+y+z=3/2 with x, y, and z all bounded between 0 and 1 (similar to the image shown in the above site.)
2 Comments
Jan
on 10 Jan 2013
@Roger: Thanks for the clarification. I've delete my answer, because I obviously had misinterpreted the term "randomly from 0 to 1".
More Answers (0)
See Also
Categories
Find more on Matrices and Arrays 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!