MATLAB: generate all possible N x N matrices with certain restrictions

Suppose you have the original N-on-N matrix, with all diagonal elements equal to zero. You want to generate all possible N-on-N matrices to:

  • all diagonal elements remain equal to zero
  • columns and rows store the sum from the original matrix
  • all elements are positive integers (including zero)

For example, for this original 3 by 3 matrix:

0 1 3 2 0 1 3 2 0 

one of the possible options is possible:

 0 0 4 3 0 0 2 3 0 
0
matrix matlab
source share
1 answer

The original idea of โ€‹โ€‹the answer, which, of course, can be further improved.

You can start thinking about a way to create diagonal zeros and where rows and columns are summed with zeros. If they can be easily built, you can get your result by adding the original matrix with all of them.

eg:.

 [ 0 1 -2 1; 1 0 -1 0; -1 2 0 -1; 0 -3 3 0]; 

You can even limit these โ€œauxiliaryโ€ matrices to the maximum maximum number of 1 and one -1 on each row / column. All the rest can be built from them.

eg.

 A = [ 0 -1 2 -1; 2 0 -2 0; -2 1 0 1; 0 0 0 0]; B = [ 0 -1 1 0; 1 0 -1 0; -1 1 0 0; 0 0 0 0]; C = [ 0 0 1 -1; 1 0 -1 0; -1 0 0 1; 0 0 0 0]; % A equals B+C 

I think this at least slightly reduces your problem. Good luck

+1
source share

All Articles