How can I transform blocks into a blockdiagonal matrix (NumPy)

后端 未结 3 757
攒了一身酷
攒了一身酷 2020-12-16 15:53

I have three same-size square matrices in NumPy. I would like to combine these to a block-diagonal matrix.

Example:

a1 = np.array([[1,1,1],[1,1,1],[1         


        
3条回答
  •  猫巷女王i
    2020-12-16 16:12

    Since these answers, numpy has added a block function

    In [695]: A=np.arange(1,10).reshape(3,3)
    In [696]: B=np.arange(10,14).reshape(2,2)
    In [698]: C = np.zeros((3,2),int)
    
    In [699]: np.block([[A,C],[C.T,B]])
    Out[699]: 
    array([[ 1,  2,  3,  0,  0],
           [ 4,  5,  6,  0,  0],
           [ 7,  8,  9,  0,  0],
           [ 0,  0,  0, 10, 11],
           [ 0,  0,  0, 12, 13]])
    

提交回复
热议问题