What is the fastest way to copy data from array b to array a, without modifying the address of array a. I need this because an external library (PyFFTW) uses a pointer to my array that cannot change.
For example:
a = numpy.empty(n, dtype=complex) for i in xrange(a.size): a[i] = b[i]
It is possible to do it without a loop?
Create a Copy of 2D Arrays Using the NumPy copy() Function NumPy offers the copy() function. The copy() function can be implemented as shown below. In the example code, we created a copy of the original array and changed the copy. However, we have retained the original copy of the array, printed in the last line.
Use numpy. copy() function to copy Python NumPy array (ndarray) to another array. This method takes the array you wanted to copy as an argument and returns an array copy of the given object. The copy owns the data and any changes made to the copy will not affect the original array.
I believe
a = numpy.empty_like(b) a[:] = b
will copy the values quickly. As Funsi mentions, recent versions of numpy also have the copyto
function.
NumPy version 1.7 has the numpy.copyto
function that does what you are looking for:
numpy.copyto(dst, src)
Copies values from one array to another, broadcasting as necessary.
See: https://docs.scipy.org/doc/numpy/reference/generated/numpy.copyto.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With