Plencho
Plencho

Reputation: 11

MATLAB: Generate all possible N x N matrices with specific constraints

Suppose you have an initial N-by-N matrix, with all diagonal elements equal to zero. You want to generate all possible N-by-N matrices such that:

For example, for this 3-by-3 initial matrix:

0 1 3
2 0 1
3 2 0

one possible variation is:

0 0 4
3 0 0
2 3 0

Upvotes: 0

Views: 112

Answers (1)

tvo
tvo

Reputation: 790

An initial idea for an answer, which can certainly be further improved.

You can start thinking of a way to make matrices that have zeros on the diagonal and where rows and columns sum to zeros. If these can be constructed easily, then you can obtain your result by adding your initial matrix with all of them.

e.g.:

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

You can even restrict these 'helper' matrices to have maximally a single 1 and a single -1 on each row/column. All others can be constructed from them.

e.g.

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 reduces your problem a bit. Good luck!

Upvotes: 1

Related Questions