Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can you do an outer summation over only one dimension of a numpy 2D array?

I have a (square) 2 dimensional numpy array where I would like to compare (subtract) all of the values within each row to each other but not to other rows so the output should be a 3D array.

matrix = np.array([[10,1,32],[32,4,15],[6,3,1]])

Output should be a 3x3x3 array which looks like:

output = [[[0,-9,22],[0,-28,-17],[0,-3,-5]], [[9,0,31],[28,0,11],[3,0,-2]], [[-22,-31,0],[17,-11,0],[5,2,0]]]

I.e. for output[0], for each of the 3 rows of matrix, subtract that row’s zeroth element from every other, for output[1] subtract each row’s first element etc.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This seems to me like a reduced version of numpy’s ufunc.outer functionality which should be possible with
tryouter = np.subtract(matrix, matrix)
and then taking some clever slice and/or transposition.

Indeed, if you do this, one finds that: output[i,j] = tryouter[i,j,i]

This looks like it should be solvable by using np.transpose to switch the 1 and 2 axes and then taking the arrays on the new 0,1 diagonal but I can’t work out how to do this with numpy diagonal or any slicing method.

Is there a way to do this or is there a simpler approach to this whole problem built into numpy?
Thanks 🙂

>Solution :

You’re close, you can do it with broadcasting:

out = matrix[None, :, :] - matrix.T[:, :, None]

Here .T is the same as np.transpose, and using None as an index introduces a new dummy dimension of size 1.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading