- one less than the number of groups of 1's. This corresponds to having one of the blocks of 1's at the beginning of the row and another at the end of the row, and distributing all of the zeros in between
- equal to the number of groups of 1's. This corresponds to having zeros at the beginning or end of the row, but not both. Use the partitions twice, once for the case of zeros at the beginning, and once for the case of zeros at the end
- equal to one more than the number of groups of 1's. This corresponds to having zeros at the beginning and end of the row.
how to do this operation on a random matrix ?
1 次查看(过去 30 天)
显示 更早的评论
if i have this matrix (n,m)
M = [ 1 0 1 1 0 0 1
1 1 0 1 0 1 0
0 1 1 0 1 1 0
1 1 1 0 0 0 0
1 1 0 0 0 0 0 ]
i want a function to generate a binary random matrix (n,m) depend on how many number of ones in each row in M is equal to number of ones in the same row in X
one solution is
X = [ 1 0 1 1 0 0 1
1 1 0 1 0 1 0
0 1 1 0 0 1 1
0 1 1 1 0 0 0
0 0 0 1 1 0 0]
another solution for the random matrix will be
x = [ 0 1 0 1 1 0 1
0 1 1 0 1 0 1
1 1 0 0 0 1 1
0 0 0 0 1 1 1
0 1 1 0 0 0 0 ]
0 个评论
采纳的回答
Walter Roberson
2016-5-4
For any given row, find the sum of the number of 1's, and subtract it from the length of the row to get the number of zeros. Then grab the partitions File Exchange Contribution and use it to find the partitions of that number of zeros, using three different lengths:
The partitions of the number of zeros correspond to the number of zeros to insert at each point.
Having generated all of the possibilities for a given number of zeros and given number of groups, pick one of the possibilities randomly.
You will probably want to pre-compute the potential partitions, and then choose randomly from that as you go along.
2 个评论
Walter Roberson
2016-5-6
See attached files.
For example,
possibilities = distribute_zeros([1 2 1],7)
The output will be all of the rows of the given length (second parameter) that meet the 1's specification of the first parameter. If the specification cannot be met then the output will be empty.
With all of these possibilities on hand, you can proceed to choose a row randomly. For the sake of efficiency, you should avoid computing the same specification multiple times -- in other words, keep a copy of the possibilities and continue to select randomly from it instead of recomputing the possibilities each time you want a new one. If you want an example of a cache system to avoid recomputing, then see the code for partitions.m in which I coded a cache system.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!