2

I essentially would like to do the opposite of this question. I have two matrixes that have been split with np.tril or np.triu and I want to recombine them into a single matrix.

A = array([[ 0. ,  0. ,  0. ],
           [ 0.1,  0. ,  0. ],
           [ 0.6,  0.5,  0. ]])

B = array([[ 0. ,  0.4,  0.8],
           [ 0. ,  0. ,  0.3],
           [ 0. ,  0. ,  0. ]])

And what I want it to look like is

array([[ 0. ,  0.4,  0.8],
       [ 0.1,  0. ,  0.3],
       [ 0.6,  0.5,  0. ]])

Is there an inbuilt numpy function to do this?

Community
  • 1
  • 1
cts
  • 1,790
  • 1
  • 13
  • 27

2 Answers2

11

You mean A+B ?

import numpy
A = numpy.array([[ 0. ,  0. ,  0. ],
           [ 0.1,  0. ,  0. ],
           [ 0.6,  0.5,  0. ]])

B = numpy.array([[ 0. ,  0.4,  0.8],
           [ 0. ,  0. ,  0.3],
           [ 0. ,  0. ,  0. ]])

print A+B

returns

array([[ 0. ,  0.4,  0.8],
       [ 0.1,  0. ,  0.3],
       [ 0.6,  0.5,  0. ]])
Antonio Ragagnin
  • 2,278
  • 4
  • 24
  • 39
1

If the values are strings, then this works as long as B is the upper triangle.

A = np.array([[ 0. ,  0. ,  0. ],
           [ '0.1**',  0. ,  0. ],
           [ 0.6,  '0.5**',  0. ]])

B = np.array([[ 0. ,  0.4,  '0.8***'],
           [ 0. ,  0. ,  0.3],
           [ 0. ,  0. ,  0. ]])

for i in range(0,len(A)):
    for j in range(0,i):
        B[i,j]=A[i,j]
            
B

Returns

array([['0.0', '0.4', '0.8***'],
       ['0.1**', '0.0', '0.3'],
       ['0.6', '0.5**', '0.0']], dtype='<U32')
J4FFLE
  • 172
  • 10