I want to create a matrix A [4×8] as follows.
The matrix A always has 1 as its diagonal. A11,A22,A33,A44 = 1
This matrix can be considered as two halves with first half being the first 4 columns and the second half being the second 4 columns like something below :
1 -1 -1 -1 1 0 0 1
A = -1 1 -1 0 0 1 0 0
-1 -1 1 0 1 0 0 0
-1 -1 -1 1 1 1 0 0
Each row in the first half can have either two or three -1’s:
- if it has two
-1‘s then that corresponding row in the second half should have one1 - if any row has three
-1‘s the second half of the matrix should have two1‘s.
The overall objective is to have the sum of each row to be 0. I need to generate all possible combinations of a matrix like this.
It will be better if the matrix with new combination be created at each iteration so that after using it I can discard it or else storing all the combinations is very space intensive. Can anybody help me ?
one possible solution I could think of is to generate all possible combinations of row1, row2, row3 and row4 and create a matrix in each iteration. Does that look feasible?
Here’s one possible solution. If you ignore the diagonal ones for the moment, you can generate all possible patterns for the other 7 values using the functions KRON, REPMAT, PERMS, UNIQUE, EYE, and ONES:
Note that this is 18 possible patterns for any given row, so your matrix
Acan have 18^4 = 104,976 possible row patterns (quite a bit). You can generate every possible 4-wise row pattern index by using the functions NDGRID, CAT, and RESHAPE:And
indexSetswill be a 104,976-by-4 matrix with each row containing one combination of 4 values between 1 and 18, inclusive, to be used as indices intorowPatternsto generate a unique matrixA. Now you can loop over each set of 4-wise row pattern indices and generate the matrixAusing the functions TRIL, TRIU, EYE, and ZEROS: