python numpy block diagonal matrix

Solutions on MaxInterview for python numpy block diagonal matrix by the best coders in the world

showing results for - "python numpy block diagonal matrix"
Kenneth
20 Aug 2016
1>>> from scipy.linalg import block_diag
2>>> A = [[1, 0],
3...      [0, 1]]
4>>> B = [[3, 4, 5],
5...      [6, 7, 8]]
6>>> C = [[7]]
7>>> P = np.zeros((2, 0), dtype='int32')
8>>> block_diag(A, B, C)
9array([[1, 0, 0, 0, 0, 0],
10       [0, 1, 0, 0, 0, 0],
11       [0, 0, 3, 4, 5, 0],
12       [0, 0, 6, 7, 8, 0],
13       [0, 0, 0, 0, 0, 7]])
14>>> block_diag(A, P, B, C)
15array([[1, 0, 0, 0, 0, 0],
16       [0, 1, 0, 0, 0, 0],
17       [0, 0, 0, 0, 0, 0],
18       [0, 0, 0, 0, 0, 0],
19       [0, 0, 3, 4, 5, 0],
20       [0, 0, 6, 7, 8, 0],
21       [0, 0, 0, 0, 0, 7]])
22>>> block_diag(1.0, [2, 3], [[4, 5], [6, 7]])
23array([[ 1.,  0.,  0.,  0.,  0.],
24       [ 0.,  2.,  3.,  0.,  0.],
25       [ 0.,  0.,  0.,  4.,  5.],
26       [ 0.,  0.,  0.,  6.,  7.]])
27