how to do this operation on a random matrix ?

조회 수: 1 (최근 30일)
Firas Al-Kharabsheh
Firas Al-Kharabsheh 2016년 5월 4일
댓글: Walter Roberson 2016년 5월 6일
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 ]

채택된 답변

Walter Roberson
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:
  1. 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
  2. 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
  3. 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.
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
Firas Al-Kharabsheh
Firas Al-Kharabsheh 2016년 5월 5일
Can you give me the code to do this
Walter Roberson
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 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