Numpy replace specific rows and columns of one array with specific rows and columns of another array

I am trying to replace specific rows and columns of a Numpy array as given below.

The values of array a and b are as below initially:

a = [[1 1 1 1]
     [1 1 1 1]
     [1 1 1 1]]

b = [[2 3 4 5]
     [6 7 8 9]
     [0 2 3 4]]

Now, based on a certain probability, I need to perform elementwise replacing of a with the values of b (say, after generating a random number, r, between 0 and 1 for each element, I will replace the element of a with that of b if r > 0.8).

How can I use numpy/scipy to do this in Python with high performance?

1 Like
i = np.random.rand(*a.shape) > 0.8
a[i] = b[i]
3 Likes

Thanks. That worked well.