I'm trying to find the elements of an NxN matrix so that given row and column sums are satisfied. The only other constraint is that the elements of the matrix are restricted to the integers 0 and 1.
This is probably a few simple lines in Matlab or Mathematica (I've hunted around on here and found a few related questions in R), but my relevant coding experience is only in constrained optimization -- I'm lost here without an objective function to minimize.
My first thought was to set it up a linear algebra problem, Ax = b, but that doesn't work because A'A is singular. (I'll provide detail on this in a comment below just in case it helps.)
My next thought was to try to "trick" the NMinimize solver in Mathematica by giving it a "dummy" objective function subject to the constraint Ax = b and the integer constraints:
x = Map[Subscript[x, #] &, Range[1, Ngrid^2]];
x = Map[Subscript[x, #] &, Range[1, Ngrid^2]];
NMinimize[{x.x\[Transpose], A.x == b && Apply[And,Thread[GreaterEqual[x, 0]]] &&
Apply[And, Thread[LessEqual[x, 1]]] && Element[x,Integers]}, x,
Method -> "DifferentialEvolution", MaxIterations -> 2000];
But it encounters the same "recursion" error.
I have a feeling this isn't the way to approach the problem. Is there some command that allows me to populate elements of a matrix according to constraints? If it doesn't generate a unique solution, is there a way to view the set of solutions?
Thanks, Dan
EDIT: Here is what I had in mind for the linear algebra solution, Ax = b:
A is a 2N x N^2 rectangular matrix of 0s and 1s. Each column corresponds to one of the N^2 elements in the solution vector x (the reshaped NxN matrix we're after). (EDIT: The first N rows correspond to the elements of x that are affected by each of the row constraints, the second N rows correspond to the elements of x that are affected by each of the column constraints.)
x is an N^2 x 1 vector of 0s and 1s (the reshaped NxN solution matrix).
b is a 2N x 1 vector of the row and column sums (the constraints).
Example:
If N=3 and we want to find x (ETA: The "x" below is the solution we're after; we don't know it ahead of time):
1 0 0
1 0 1
0 1 0
We could write A =
1 1 1 0 0 0 0 0 0
0 0 0 1 1 1 0 0 0
0 0 0 0 0 0 1 1 1
1 0 0 1 0 0 1 0 0
0 1 0 0 1 0 0 1 0
0 0 1 0 0 1 0 0 1
And z =
1
2
1
2
1
1
Solving isn't as simple as x = inv(A'A)*A'z, because A'A doesn't have an inverse.
Thanks in advance for any help you can provide!
sum
of rows and columns that match a certain value? Or are you trying to find a subset of theA
matrix that would create a newNxN
matrix with the proper values? Really confused by what you want... – Matt