1

I have source and destination numpy arrays, say

dest = np.arange(1000)
src = np.random(500)

I want to copy across the src to the dest array overwriting the first 50 numbers, then all the numbers starting at index 100 to 150, then from 200 to 250 and so on till 900 to 950.

To calculate the index I use the following:

index = np.reshape([100*i+np.arange(50) for i in range(10)],-1)

then I just use

dest[index]=src

to copy across the remaining elements (with another src, say src2), i just adust the index

index2 = np.reshape([50+100*i+np.arange(50) for i in range(10)],-1)
dest[index2]=src2

I'm pretty sure there's a more elegant/efficient way of achieving this without having to explicitly build the indexes.

Is there a better way to perform the copy?

2 Answers 2

1

dest.reshape(-1,50)[::2] = src.reshape(-1,50)

And for src2:

dest.reshape(-1,50)[1::2] = src2.reshape(-1,50)

Sign up to request clarification or add additional context in comments.

Comments

1

I guess the easiest would be to reshape both into matrices

dest = dest.reshape((10, 100))
src = src.reshape((10, 50))

and use matrix indexing

dest[:,:50] = src

The idea is to create a 100 x 10 matrix of dest and replace the left half with src converted to a 50 x 10 matrix.


Edit

@Haminaa's answer is slightly faster.

%timeit dest.reshape((10, -1))[:,:50] = src.reshape((10, -1))

The slowest run took 11.54 times longer than the fastest. This could mean that an intermediate result is being cached. 1000000 loops, best of 3: 1.55 µs per loop

%timeit dest.reshape(-1,50)[::2] = src.reshape(-1,50)

The slowest run took 12.97 times longer than the fastest. This could mean that an intermediate result is being cached. 1000000 loops, best of 3: 1.38 µs per loop

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.