How can I generate a random binary matrix under some conditions?

Hi, I want to generate a 4*960 binary matrix but there are some conditions. For example in row 1, in the first 60 elements, maximum number of consecutive 0 arrays should be 5, in the second 60 elements maximum number of consecutive 0 arrays should be 10 and so on. I mean maximum number of 0 elements between two consecutive 1s should be less than a maximum number. And how can I get the number of 1 elements as an out put. I don't mean the count of them, I want to know which elements have a 1 value in a row. Would you please help me with this problems?

6 commentaires

Image Analyst
Image Analyst le 29 Sep 2016
Modifié(e) : Image Analyst le 29 Sep 2016
If a run of zeros crosses 60, like say it goes from index 55 to 65, is that counted as being two consecutive arrays or just one? To get 5 runs of zeros, you need to place 4 runs of ones somewhere in the 60 elements. Have you looked at the randi() and randperm() functions?
Matt J
Matt J le 29 Sep 2016
Modifié(e) : Matt J le 29 Sep 2016
Do you care about the distribution of the randomization? The restrictions you mention means that each row of data is constrained to some hyper-polyhedron in R^960. Do you need the randomization to draw uniformly from this polyhedron?
Thank you so much for your help. No, it doesn't count. Every 60 consecutive elements are considered separately. I know that I can use round(rand(4,960)). I just don't know how to define the conditions.
No the distribution isn't important. just some random 0s and 1s.
Matt J
Matt J le 29 Sep 2016
Modifié(e) : Matt J le 29 Sep 2016
No the distribution isn't important. just some random 0s and 1s.
If the probability distribution really doesn't matter to you, then choose, with probability 1, the matrix
A=ones(4,960);
Since it has no zeros, it respects your bounds on the maximum number of consecutive zeros.
I want to know which elements have a 1 value in a row.
Use the FIND command.
Some months ago someone was working on a puzzle involving a binary matrix in which they were given vectors of counts of ones for rows and for columns, and the puzzle was to find matrices that could meet those run counts. I provided generation code for all cases of known length and given counts of runs. Unfortunately I cannot seem to find that series of postings at the moment.

Connectez-vous pour commenter.

Réponses (2)

John D'Errico
John D'Errico le 29 Sep 2016
This is just blocks of 60 elements. Just generate a random sample using rand, then discard it if it fails your goal. Do that for each block. Since it appears that the probability of a 1 will differ in each block, you will need to adjust the probability of a 1 occurring according to those odds. But it is not at all clear as to exactly what your constraints are.
There will probably be no better solution in general. Why not? Your constraint is a bit unusual, but disregarding that, a solution that would be more sophisticated will also be far less efficient than just sample and discard until you are happy with any given block of 60.
Matt J
Matt J le 29 Sep 2016
Modifié(e) : Matt J le 29 Sep 2016
The following generates 60 elements x(i), i=1...60, with zmax=5 maximum consecutive zeros. You can repeat this iteratively for any number of blocks, block sizes, and zmaxes that you want. It uses my FUNC2MAT ( Download ) File Exchange submission.
N=60;
zmax=5;
A=-func2mat(@(x) conv(x,ones(1,zmax+1),'valid'), zeros(N,1) );
b=-ones(size(A,1),1);
opts=optimoptions(@intlinprog,'MaxNodes',1);
x = intlinprog(rand(1,N),ones(1,N),A,b,[],[],zeros(1,N),ones(1,N),opts);
The result is non-trivially stochastic, unlike in my comment here. However, because of my comment, I still cannot help but wonder if you have under-specified the requirements.

3 commentaires

what if zmax is also different for each row?
Matt J
Matt J le 29 Sep 2016
Modifié(e) : Matt J le 29 Sep 2016
So what if it is? As I said, you can place the code in a loop and vary any of the parameters you choose in the loop.
Thank you so much!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Random Number Generation dans Centre d'aide et File Exchange

Question posée :

le 29 Sep 2016

Commenté :

le 29 Sep 2016

Community Treasure Hunt

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

Start Hunting!

Translated by