Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy merge upper and lower triangular

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?

like image 855
cts Avatar asked Aug 13 '26 21:08

cts


1 Answers

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. ]])
like image 136
Antonio Ragagnin Avatar answered Aug 16 '26 18:08

Antonio Ragagnin