Is there any fast and simple way to insert a small matrix into the center (or any other x,y index) of another, biger matrix using numpy or scipy?
That is, say I have matrix
A = [1 2]
[3 4]
and matrix
B = [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]
I want to insert A into the center of B as so:
C = [0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 1 2 0 0]
[0 0 3 4 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
You can use numpy's slice notation.
nb = B.shape[0]
na = A.shape[0]
lower = (nb) // 2 - (na // 2)
upper = (nb // 2) + (na // 2)
B[lower:upper, lower:upper] = A
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With