Replace sub part of matrix by another small matrix in numpy

Here is how you can do it:

>>> A[3:5, 3:5] = B
>>> A
array([[ 1. ,  1. ,  1. ,  1. ,  1. ],
       [ 1. ,  1. ,  1. ,  1. ,  1. ],
       [ 1. ,  1. ,  1. ,  1. ,  1. ],
       [ 1. ,  1. ,  1. ,  0.1,  0.2],
       [ 1. ,  1. ,  1. ,  0.3,  0.4]])

In general, for example, for non-contiguous rows/cols use numpy.putmask(a, mask, values) (Sets a.flat[n] = values[n] for each n where mask.flat[n]==True)

For example

In [1]: a = np.zeros((3, 3))
Out [1]: a
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]])

In [2]: values = np.ones((2, 2))
Out [2]: values
array([[1., 1.],
       [1., 1.]])

In [3]: mask = np.zeros((3, 3), dtype=bool)
In [4]: mask[0,0] = mask[0,1] = mask[1,1] = mask[2,2] = True

Out [4]: mask
array([[ True,  True, False],
       [False,  True, False],
       [False, False,  True]])

In [5] np.putmask(a, mask, values)
Out [5] a
array([[1., 1., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])

For the first one:

In [13]: A[-B.shape[0]:, -B.shape[1]:] = B                              

In [14]: A
Out[14]: 
array([[ 1. ,  1. ,  1. ,  1. ,  1. ],                                  
       [ 1. ,  1. ,  1. ,  1. ,  1. ],                                  
       [ 1. ,  1. ,  1. ,  1. ,  1. ],                                  
       [ 1. ,  1. ,  1. ,  0.1,  0.2],                                  
       [ 1. ,  1. ,  1. ,  0.3,  0.4]])   

For second:

In [15]: A = np.ones((5,5))                                             

In [16]: A[:B.shape[0], -B.shape[1]:] = B                               

In [17]: A
Out[17]: 
array([[ 1. ,  1. ,  1. ,  0.1,  0.2],                                  
       [ 1. ,  1. ,  1. ,  0.3,  0.4],                                  
       [ 1. ,  1. ,  1. ,  1. ,  1. ],                                  
       [ 1. ,  1. ,  1. ,  1. ,  1. ],                                  
       [ 1. ,  1. ,  1. ,  1. ,  1. ]])