What l need to solve ?
Build a binary matrix from a list of indices. Here is how l proceed but l would like to get an efficient way to do that avoiding loops
Input :
list_indices =[
[0,3,4],
[2,1,0],
[3,5]
]
Expected output :
results=[
[0,1,1,1,1,0],
[1,0,1,0,0,0],
[0,0,0,0,0,0],
[1,0,0,0,1,1],
[1,0,0,1,0,0],
[0,0,1,0,0,0],
]
results correspond to a binary adjacency (symmetric) matrix constructed from a list of indices. 1 in results correspond to a pair of indices belonging to the same row of list_indices.
The pairs from list_indices are :
row 1 : (0,3), (3,0), (0,4),(4,0), (3,4), (4,3)
row 2 : (0,1), (1,0), (2,0), (0,2),(1,2), (2,1)
row 3 : (3,5), (5,3)
number of column and number of rows in results = np.max(list_indices)+1=6
What l have tried ?
results=np.zeros((np.max(list_indices)+1,np.max(list_indices)+1))
for pair in itertools.combinations(list_indices, r=2) :
results[pair[0],pair[1]]=results[pair[1],pair[0]]=1.0
What is the efficient way to build that ? (avoiding loops)
itertools.combinations returns a list of pairs which are then used to fill the matrix results. Since the matrix is symmetric, itertools.combinations provides a list of pairs corresponding to the upper diagonal matrix. diagonal is set to zero
