Filter löschen
Filter löschen

Create a random matrix?

2 Ansichten (letzte 30 Tage)
Pham
Pham am 30 Dez. 2013
Kommentiert: Pham am 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 Kommentar
Pham
Pham am 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!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Roger Stafford
Roger Stafford am 30 Dez. 2013
Bearbeitet: Roger Stafford am 30 Dez. 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.
  1 Kommentar
Pham
Pham am 31 Dez. 2013
OK. Thank you very much.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

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

Melden Sie sich an, um zu kommentieren.


Amit
Amit am 31 Dez. 2013
Bearbeitet: Amit am 31 Dez. 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 Kommentare
Image Analyst
Image Analyst am 31 Dez. 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 am 31 Dez. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by