4

I have a number of different size arrays with a common index.

For example,

Arr1 = np.arange(0, 1000, 1).reshape(100, 10)
Arr2 = np.arange(0, 500, 1).reshape(100,5)

Arr1.shape = (100, 10)
Arr2.shape = (100, 5)

I want to add these together into a new Array, Arr3 which is three dimensional. e.g.

Arr3 = Arr1 + Arr2
Arr3.shape = (100, 10, 5)

Note, in this instance the values should allign e.g.

Arr3[10, 3, 2] =  Arr1[10, 3] + Arr2[10, 2]

I have been attempting to use the following method

test = Arr1.copy()
test = test[:, np.newaxis] + Arr2

Now, I've been able to make this work when adding two square matrices together.

m = np.arange(0, 100, 1)
[x, y] = np.meshgrid(x, y)
x.shape = (100, 100)

test44 = x.copy()
test44 = test44[:, np.newaxis] + x
test44.shape = (100, 100, 100)
test44[4, 3, 2] = 4
x[4, 2] = 2
x[3, 2] = 2

However, in my actual program I will not have square matrices for this issue. In addition this method is extremely memory intensive as evidenced when you begin moving up the number of dimensions as follows.

test44 = test44[:, :, np.newaxis] + x
test44.shape = (100, 100, 100, 100)

# Note this next command will fail with a memory error on my computer.
test44 = test44[:, :, :, np.newaxis] + x

So my question has two parts:

  1. Is it possible to create a 3D array from two differently shaped 2D array with a common "shared" axis.
  2. Is such a method extensible at higher order dimensions?

Any assistance is greatly appreciated.

1 Answer 1

4

Yes what you're trying to do is called broadcasting, it's done automatically by numpy if the inputs have the right shapes. Try this:

Arr1 = Arr1.reshape((100, 10, 1))
Arr2 = Arr2.reshape((100, 1, 5))
Arr3 = Arr1 + Arr2

I've found this to be a really good introduction to broadcasting which should show you how to extend this kind of behavior to n dimensions.

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

5 Comments

So if I wanted to do a four dimensional broadcast, say Arr4 = Arr1 + Arr2 + Arr3 I would need to reshape all of them to four dimensions first?
More than just the number of dimensions, you should figure out what the shape of the final result should be and reshape the input arrays so that they can broadcast to the final shape. For example if the final shape should be (2, 3, 4, 100) you might try: Arr1.shape = (2, 1, 1, 100); Arr1.shape = (1, 3, 1, 100); Arr1.shape = (1, 1, 4, 100). In this case you can technically leave off the leading 1s, but I like to include them to be explicit.
Great thanks to know. I've been able to broadcast successfully, however I now hit memory issues when I start moving into higher dimensions so I think I may need to develop a less "brute force" method. Thank you for your help though.
There appears to be a dead link here.
Or alternatively, index with None (or np.newaxis) to insert a new dimension to broadcast along, e.g. Arr3 = Arr1[:, :, None] + Arr2[:, None, :].

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.