In this question it is explained how to access the lower
and upper
triagular parts of a given matrix, say:
m = np.matrix([[11, 12, 13],
[21, 22, 23],
[31, 32, 33]])
Here I need to transform the matrix in a 1D array, which can be done doing:
indices = np.triu_indices_from(m)
a = np.asarray( m[indices] )[-1]
#array([11, 12, 13, 22, 23, 33])
After doing a lot of calculations with a
, changing its values, it will be used to fill a symmetric 2D array:
new = np.zeros(m.shape)
for i,j in enumerate(zip(*indices)):
new[j]=a[i]
new[j[1],j[0]]=a[i]
Returning:
array([[ 11., 12., 13.],
[ 12., 22., 23.],
[ 13., 23., 33.]])
Is there a better way to accomplish this? More especifically, avoiding the Python loop to rebuild the 2D array?
a
before returning to the symmetric2D-array
– Octameter