1

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?

1 Answer 1

4

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]])
Sign up to request clarification or add additional context in comments.

2 Comments

How did you get all those nan in A?
ops, didn't run it and didn't remember how np.empty works. Fixed it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.