If I have a matrix:
0 0 3 4
4 3 2 0
2 0 2 0
I want to extract the non-zero elements into some batches, with respect to a rule: if an element is already taken, the other element in the same row/column is not allowed. So the extracted matrix will be:
1st batch:
0 0 3 0
4 0 0 0
0 0 0 0
2nd batch:
0 0 0 4
0 3 0 0
0 0 2 0
3rd batch:
0 0 0 0
0 0 2 0
2 0 0 0
Any other combination of batches is also accepted, as long as all non-zero elements are covered, and the rule is conformed. How would you do that in MATLAB/Octave?
Gunther was already on the right track. You want to select an element, if
The following code solves the problem:
Note however that the returned solution is not optimal because its 2nd batch is
which means that the remaining matrix elements are from the same column:
Unfortunately, you cannot draw them in the same batch. So you end up with four batches instead of just three.
Edit: Probably, it is a good idea, to select first those elements, which appear in rows/columns with a lot of non-zeros. For example, one could use these weights
The following algorithm
returns those batches.
So it worked at least for this small test case.