You could use np.put
:
In [74]: np.put(A, np.arange(len(B)), B)
In [75]: A
Out[75]:
array([[ 1., 2., 3.],
[ 4., 5., 6.],
[ 7., 8., 9.],
[ 0., 0., 0.],
[ 0., 0., 0.]])
or assign to A.flat
:
A.flat[:len(B)] = B
or (as ali_m points out) if A
is C-contiguous, you could use A.ravel
:
assert A.flags['C_CONTIGUOUS']
A.ravel()[:len(B)] = B
If A
is not C-contiguous, then A.ravel()
returns a copy. Modifying the copy does not modify A
. Examples of non-C-contiguous arrays include slices with non-unit step size such as A[::2]
and F-contiguous arrays such as np.asarray(A, order='F')
.
You can use Python's timeit
module to benchmark pieces of code. If you have IPython, a particularly convenient way to benchmark code snippets is to use its %timeit
"magic" function:
In [83]: A = np.zeros((500, 300))
In [84]: B = np.tile(np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]),1000)
In [88]: %timeit A = np.zeros((500, 300)); A.flat[:len(B)] = B
10000 loops, best of 3: 158 µs per loop
In [89]: %timeit A = np.zeros((500, 300)); np.put(A, np.arange(len(B)), B)
1000 loops, best of 3: 238 µs per loop
In [18]: %timeit A = np.zeros((500, 300)); A.ravel()[:len(B)] = B
10000 loops, best of 3: 91.6 µs per loop
which suggests that A.ravel()[:len(B)] = B
is the faster than A.flat[:len(B)] = B
which is faster than np.put(A, np.arange(len(B)), B)
.
Benchmarks can vary from machine to machine depending on many factors such as
hardware, OS, version of software, or how the software was compiled. One piece
of code could be faster than an alternative piece of code for small arrays but
slower for larger arrays. So when you really want to benchmark, be sure to test
the code on input, hardware and software closest to your actual use case, while
keeping in mind the futility of pre-optimization.