Create a random matrix?

6 views (last 30 days)
Pham
Pham on 30 Dec 2013
Commented: Pham on 2 Jan 2014
I want to create a random matrix HM (a,b), it satisfies the following conditions:
1. The value of HM is a nonnegative integer from 0 to c.
2. The total value of the elements in a row is less than or equal to d.
you are way that uses loop to made that.
Thanks!
  1 Comment
Pham
Pham on 2 Jan 2014
Thanks for everyone's help. After creating the matrix HM, now I want to create 1 vector whose elements are randomly selected from the HM with probability P, this vector satisfies the above constraints. Thank you!

Sign in to comment.

Accepted Answer

Roger Stafford
Roger Stafford on 30 Dec 2013
Edited: Roger Stafford on 30 Dec 2013
HM = zeros(a,b);
for k = 1:a
t = zeros(c,b);
t(randperm(b*c,d)) = 1;
HM(k,:) = sum(t,1);
end
Note: This is not a rejection method.

More Answers (2)

ES
ES on 30 Dec 2013
This should do!
HM=rand(a,b)*d/a;
  1 Comment
Pham
Pham on 30 Dec 2013
The value of HM is a nonnegative integer from 0 to c

Sign in to comment.


Amit
Amit on 31 Dec 2013
Edited: Amit on 31 Dec 2013
@Roger: This is a very interesting approach and got me curious. However I think this will not satisfy the random number scenario as requested in the question.
For example, lets take a scenario for a = 5, b = 4, c = 4 and d = 4. In this scenario, the probability of [1 2 0 0] and [1 3 0 0] are same. However, with the approach suggested here, the chances of [1 2 0 0] > [1 3 0 0]. On few test runs, I did not even see a single 3 in the matrix.
The rejection method will atleast guarantee the equal probability of these two scenarios. I also admit that rejection method will be tedious for long matrixes and take a lot of time for the cases where b >> d.
  7 Comments
Image Analyst
Image Analyst on 31 Dec 2013
Well sure you can cherry pick combinations of "random" numbers that work, but the distribution is different. I don't see it as being any different than the frequently asked question where people want Gaussian distributed random numbers between 1 and 5. Sure you can use randn and get a bunch of numbers in that range. But people answering always point out that they aren't truly normally distributed even though they came from the randn() function because you're not allowing anything less than 1 or more than 5. So if you took a trillion numbers, you wouldn't have tails on the distribution that go to -infinity and +infinity - the tails/distribution would be clipped.
Roger Stafford
Roger Stafford on 31 Dec 2013
@Amit & Image Analyst: Yes, you are both right. The method I used very definitely does not give equal probabilities to the different solutions. Mea culpa! It favors the "central" values clustered near d/b over those farther away. Using a rejection method which chooses each number uniformly from 0 to c regardless of their sum and then discards those whose sum is too large will avoid that difficulty. However, for large values of b and c, and small d, this unfortunately can lead to some extremely high rejection rates.
On the other hand based on my experience in writing 'randfixedsum', I think avoiding rejection and at the same time having equal probabilities for each successful set of numbers would be a difficult undertaking. The trouble is that for large values of b, c, and d there is an enormous complexity to the space of all successful combinations. To take a simple example, let b = 3, c = 9, and d = 13. Without the d limit there would be an uncomplicated 10 x 10 x 10 cube of possible combinations, but invoking this d limit cuts this cube at a certain hexagonal face and we are faced with the task of determining which of the 1000 combinations lie inside the volume that is at or behind this hexagonal face and selecting each with equal probability. There are in fact 500 such combinations and it is already a slightly messy task to use a random process to select among them uniformly. With large values for b this problem becomes wildly complicated in the resulting discrete versions of the world of b-dimensional polytopes.

Sign in to comment.

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!