How can I generate a random binary matrix under some conditions?
5 views (last 30 days)
Show older comments
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 Comments
Matt J
on 29 Sep 2016
Edited: Matt J
on 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.
Walter Roberson
on 29 Sep 2016
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.
Answers (2)
John D'Errico
on 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
on 29 Sep 2016
Edited: Matt J
on 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 Comments
See Also
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!