Search code examples
pythondiagram

Create Block Diagram with two Blocks and rest of 0's


I need to make a block diagram where I have two diagonal blocks, with all other number zeros. I need it to be a 20x20 matrix.

What I have so far is

import numpy as np
T = 0.6
B = np.array([[T, np.sqrt(1-T)], [-np.sqrt(1-T), T]])

B_diag = np.kron(np.eye(10,dtype=int),B)

`

which gives a 20x20 matrix but with B on all diagonals. I am new to coding so unsure how to do this. Thanks for the help!

Required output:

|C1|C2|C3|C4|C5|C6|C7|C8|
|--|--|--|--|--|--|--|--|
|1|2|0|0|0|0|0|0|0|0|0|0|
|3|4|0|0|0|0|0|0|0|0|0|0|
|0|0|1|2|0|0|0|0|0|0|0|0|
|0|0|3|4|0|0|0|0|0|0|0|0|
|0|0|0|0|0|0|0|0|0|0|0|0|
|0|0|0|0|0|0|0|0|0|0|0|0|
|0|0|0|0|0|0|0|0|0|0|0|0|
|0|0|0|0|0|0|0|0|0|0|0|0|

with 20 rows and 20 columns and where 1 = 4 = T, 2 = sqrt(T), 3 = -sqrt(T). The C's are just there for the formatting required by stack.


Solution

  • You just need to splice the 4x4 array into a larger 20x20 array of zeros.

    
    import numpy as np
    T = 0.6
    B = np.array([[T, np.sqrt(1-T)], [-np.sqrt(1-T), T]])
    
    B_diag = np.kron(np.eye(2,dtype=int),B)
    
    M = np.zeros((20,20))
    
    M[:4, :4] = B_diag
    
    print(M)
    

    And you get the expected output in the first 5x5 block

    [[ 0.6         0.63245553  0.          0.          0.        ]
     [-0.63245553  0.6        -0.          0.          0.        ]
     [ 0.          0.          0.6         0.63245553  0.        ]
     [-0.          0.         -0.63245553  0.6         0.        ]
     [ 0.          0.          0.          0.          0.        ]]