- 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 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Firas Al-Kharabsheh
am 4 Mai 2016
Kommentiert: Walter Roberson
am 6 Mai 2016
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 Kommentare
Akzeptierte Antwort
Walter Roberson
am 4 Mai 2016
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 Kommentare
Walter Roberson
am 6 Mai 2016
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.
Weitere Antworten (0)
Siehe auch
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!