赵天阳
赵天阳

Reputation: 109

Make a binary array symmetric - NumPy / Python

How to convert a non-symmetric numpy matrix to be a symmetric matrix?

the request is : if the element a[ i ][ j ] of upper triangle(above the diagonal) is 1, the element of lower triangle a[j][i] should be changed to 1.

The same for the lower triangle, if a[ i ][ j ] =1, the symmetric element a[ j ][ i ] in upper triangle should be 1.

Just care about change the element from 0 to 1,don;t change it from 0 to 1.

I can solve it with two or more for loops, but I want to know how to solve it in a more pythonic way with higher time efficiency?

Thanks in advance !

For example:

a= np.array ([[0, 0, 1, 0, 1],
              [0, 0, 0, 1, 1],
              [0, 0, 0, 1, 1],
              [1, 1, 0, 0, 0],
              [1, 1, 0, 1, 0]])

the result should be :

a= np.array ([[0, 0, 1, 1, 1],
              [0, 0, 0, 1, 1],
              [1, 0, 0, 1, 1],
              [1, 1, 1, 0, 1],
              [1, 1, 1, 1, 0]])

Upvotes: 1

Views: 199

Answers (1)

Divakar
Divakar

Reputation: 221654

One way purely with masking -

In [40]: m = a==1

In [41]: (m | m.T).view('i1')
Out[41]: 
array([[0, 0, 1, 1, 1],
       [0, 0, 0, 1, 1],
       [1, 0, 0, 1, 1],
       [1, 1, 1, 0, 1],
       [1, 1, 1, 1, 0]], dtype=int8)

Another with addition -

In [55]: ((a.T + a)>0).view('i1')
Out[55]: 
array([[0, 0, 1, 1, 1],
       [0, 0, 0, 1, 1],
       [1, 0, 0, 1, 1],
       [1, 1, 1, 0, 1],
       [1, 1, 1, 1, 0]], dtype=int8)

Another with just bitwise-OR-ing -

In [57]: (a.T | a)
Out[57]: 
array([[0, 0, 1, 1, 1],
       [0, 0, 0, 1, 1],
       [1, 0, 0, 1, 1],
       [1, 1, 1, 0, 1],
       [1, 1, 1, 1, 0]])

Upvotes: 1

Related Questions