3

I'm making the transition from MATLAB to Numpy and feeling some growing pains.

I have a 3D array, lets say it's 3x3x3 and I want the scalar sum of each plane. In matlab, I would use:

sum_vec = sum(3dArray,3);

TIA wbg

EDIT: I was wrong about my matlab code. Matlab only vectorizes in one dim, so a loop wold be required. So numpy turns out to be more elegant...cool.

MATLAB
for i = 1:3
    sum_vec(i) = sum(sum(3dArray(:,:,i));
end

5 Answers 5

7

You can do

sum_vec = np.array([plane.sum() for plane in cube])

or simply

sum_vec = cube.sum(-1).sum(-1)

where cube is your 3d array. You can specify 0 or 1 instead of -1 (or 2) depending on the orientation of the planes. The latter version is also better because it doesn't use a Python loop, which usually helps to improve performance when using numpy.

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

2 Comments

That's it...I'm totally in matlab mode....I assumed there was a shorter command, but your snippet is pretty intuitive.Thanks everyone.
@wbg seberg mentions that a shorter command will be available in newer versions of numpy, which is nice.
4

You should use the axis keyword in np.sum. Like in many other numpy functions, axis lets you perform the operation along a specific axis. For example, if you want to sum along the last dimension of the array, you would do:

import numpy as np
sum_vec = np.sum(3dArray, axis=-1)

And you'll get a resulting 2D array which corresponds to the sum along the last dimension to all the array slices 3dArray[i, k, :].

UPDATE

I didn't understand exactly what you wanted. You want to sum over two dimensions (a plane). In this case you can do two sums. For example, summing over the first two dimensions:

sum_vec = np.sum(np.sum(3dArray, axis=0), axis=0)

4 Comments

Thanks for responding. I need to be more clear. I would like a vector of scalars, for each plane in the 3dArray, where each scalar is the sum of the entire plane. Therefore, for a 3x3x3 array I would have sum_vec = ([43, 123, 455])
@wbg so you want to do the sum twice (your matlab code does not do that as well). On new numpy you can (will be) able to do array.sum((1,2)) as well, to directly sum along two axes at once.
yeah, you're right...! that's sad, I've been doing regular python for awhile, forgot that matlab only vectorizes in one dim, thus a 3d array must be looped.
@wbg, right. I updated the answer to now sum over the planes.
0

Instead of applying the same sum function twice, you may perform the sum on the reshaped array:

a = np.random.rand(10, 10, 10) # 3D array
b = a.view()
b.shape = (a.shape[0], -1)
c = np.sum(b, axis=1)

The above should be faster because you only sum once.

Comments

0
sumvec= np.sum(3DArray, axis=2)

or this works as well

sumvec=3DArray.sum(2)

Remember Python starts with 0 so axis=2 represent the 3rd dimension.

https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.sum.html

Comments

0

If you're trying to sum over a plane (and avoid loops, which is always a good idea) you can use np.sum and pass two axes as a tuple for your argument. For example, if you have an (nx3x3) array then using

np.sum(a, (1,2))

Will give an (nx1x1), summing over a plane, not a single axis.

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.