user3576279
user3576279

Reputation:

How to replace values of an array with another array using numpy in Python

I want to place the array B (without loops) on the array A with starting index A[0,0]

A=np.empty((3,3))
A[:] = np.nan
B=np.ones((2,2))

The result should be:

array([[  1.,   1.,  nan],
       [  1.,   1.,  nan],
       [ nan,  nan,  nan]])

I tried numpy.place(arr, mask, vals) and numpy.put(a, ind, v, mode='raise') but I have to find the mask or all indexes.

How to do that?

Upvotes: 1

Views: 3642

Answers (1)

iacolippo
iacolippo

Reputation: 4513

Assign it where you want using indexing

import numpy as np
A = np.empty((3,3))
a[:] = np.nan
B = np.ones((2,2))
A[:B.shape[0], :B.shape[1]] = B



array([[1.00000000e+000, 1.00000000e+000, nan],
       [1.00000000e+000, 1.00000000e+000, nan],
       [nan, nan, nan]])

Upvotes: 4

Related Questions