python - best way to stack matrices

Viewed 85

i have two matrices

[[0,0,0],
 [0,0,0],
 [0,0,0]]

[[1,1,1],
 [1,1,1],
 [1,1,1]]

I would like them v-stacked and h-stacked like this

[[0,0,0,1,1,1],
 [0,0,0,1,1,1],
 [0,0,0,1,1,1],
 [1,1,1,0,0,0],
 [1,1,1,0,0,0],
 [1,1,1,0,0,0]]

what is the best way to do this?

1 Answers

You could use np.block for this:

A = np.array([[0,0,0],
              [0,0,0],
              [0,0,0]])

B = np.array([[1,1,1],
              [1,1,1],
              [1,1,1]])

np.block([[A, B], [B, A]])

Output:

array([[0, 0, 0, 1, 1, 1],
       [0, 0, 0, 1, 1, 1],
       [0, 0, 0, 1, 1, 1],
       [1, 1, 1, 0, 0, 0],
       [1, 1, 1, 0, 0, 0],
       [1, 1, 1, 0, 0, 0]])
Related