Skip to content
Advertisement

How to copy a 2D array into a 3rd dimension, N times?

I’d like to copy a numpy 2D array into a third dimension. For example, given the 2D numpy array:

JavaScript

convert it into a 3D matrix with N such copies in a new dimension. Acting on arr with N=3, the output should be:

JavaScript

Advertisement

Answer

Probably the cleanest way is to use np.repeat:

JavaScript

Having said that, you can often avoid repeating your arrays altogether by using broadcasting. For example, let’s say I wanted to add a (3,) vector:

JavaScript

to a. I could copy the contents of a 3 times in the third dimension, then copy the contents of c twice in both the first and second dimensions, so that both of my arrays were (2, 2, 3), then compute their sum. However, it’s much simpler and quicker to do this:

JavaScript

Here, a[..., None] has shape (2, 2, 1) and c[None, None, :] has shape (1, 1, 3)*. When I compute the sum, the result gets ‘broadcast’ out along the dimensions of size 1, giving me a result of shape (2, 2, 3):

JavaScript

Broadcasting is a very powerful technique because it avoids the additional overhead involved in creating repeated copies of your input arrays in memory.


* Although I included them for clarity, the None indices into c aren’t actually necessary – you could also do a[..., None] + c, i.e. broadcast a (2, 2, 1) array against a (3,) array. This is because if one of the arrays has fewer dimensions than the other then only the trailing dimensions of the two arrays need to be compatible. To give a more complicated example:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement