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

3 次查看(过去 30 天)
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 个评论
Matt J
Matt J 2016-9-29
编辑:Matt J 2016-9-29
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
Walter Roberson 2016-9-29
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.

请先登录,再进行评论。

回答(2 个)

John D'Errico
John D'Errico 2016-9-29
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 2016-9-29
编辑:Matt J 2016-9-29
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 个评论
Matt J
Matt J 2016-9-29
编辑:Matt J 2016-9-29
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by