Say I have a matrix (array) where elements are in the set {0,1} subject to the constraint that all rows and columns each sum to 1.
matches = np.array([[0,0,1],
[1,0,0],
[0,1,0]])
Is there a means to generate all possible such matrices that satisfy the above constraint?
Here's a naive attempt taking inspirational from a vaguely related question/answer,
import itertools
a = ['A','B','C']
b = ['X','Y','Z']
def get_permutations(a,b):
index_map = {val:idx for idx,val in enumerate(a)}
index_map.update({val:idx for idx,val in enumerate(b)})
perms = [list(zip(x,b)) for x in itertools.permutations(a,len(b))]
possible = []
for p in perms:
temp_arr = np.zeros([len(a),len(b)])
for tup in p:
x,y = index_map[tup[0]], index_map[tup[1]]
temp_arr[x,y] = 1
possible.append(temp_arr)
return possible
get_permutations(a,b)
>>>
[array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]]), array([[1., 0., 0.],
[0., 0., 1.],
[0., 1., 0.]]), array([[0., 1., 0.],
[1., 0., 0.],
[0., 0., 1.]]), array([[0., 0., 1.],
[1., 0., 0.],
[0., 1., 0.]]), array([[0., 1., 0.],
[0., 0., 1.],
[1., 0., 0.]]), array([[0., 0., 1.],
[0., 1., 0.],
[1., 0., 0.]])]
My question is: Is there a more condensed or otherwise faster way to return the list of arrays meeting the constraint mentioned above?