Python Forum
How to sum elements of same position in n dimensional array
Thread Rating:
  • 4 Vote(s) - 3.25 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to sum elements of same position in n dimensional array
#1
Hi, guys,

I have a numpy array of (2,100,2) dimensions, that I want to sum elements of the same position. In order to clarify my doubts, I wrote the example below where I substituted the numbers by letters with dimensions (2,2,2): 

[[[a b]
  [c d]]
 [[a b]
  [c d]]]
So, I look for a way to sum a+a, b+b, c+c, d+d, and obtain an array with dimensions (2,2,1). Here's the desired output:

[[a+a  b+b]
 [c+c  d+d]]
I'd like something that I can extrapolate to cases with dimensions (2,100,X), where X can be numbers from 2 to 10.
Thanks !!!
Reply
#2
It seems that you can use .sum() method with axis parameter.

In [1]: import numpy as np

In [2]: a = np.array([[[1,2], [3,4]], [[10, 20], [30, 40]]])

In [3]: a
Out[3]: 
array([[[ 1,  2],
        [ 3,  4]],
       [[10, 20],
        [30, 40]]])

In [4]: a.sum(axis=0)
Out[4]: 
array([[11, 22],
       [33, 44]])
As you can see, result has dimensionality reduced to (2, 2), if you want result with shape (2, 2, 1), you need to reshape it or add new dimension:
In [5]: a.sum(axis=0)[..., None]
Out[5]: 
array([[[11],
        [22]],
       [[33],
        [44]]])
Reply
#3
(May-11-2017, 09:12 AM)zivoni Wrote: It seems that you can use .sum() method with axis parameter.

In [1]: import numpy as np

In [2]: a = np.array([[[1,2], [3,4]], [[10, 20], [30, 40]]])

In [3]: a
Out[3]: 
array([[[ 1,  2],
        [ 3,  4]],
       [[10, 20],
        [30, 40]]])

In [4]: a.sum(axis=0)
Out[4]: 
array([[11, 22],
       [33, 44]])
As you can see, result has dimensionality reduced to (2, 2), if you want result with shape (2, 2, 1), you need to reshape it or add new dimension:
In [5]: a.sum(axis=0)[..., None]
Out[5]: 
array([[[11],
        [22]],
       [[33],
        [44]]])
Thank you !!! This works perfectly for me.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Formula with elements of list - If-condition regarding the lists elements lewielewis 2 2,702 May-08-2020, 01:41 PM
Last Post: nnk
  define certain array elements+display with digits lukezo 0 1,232 Apr-10-2020, 05:03 PM
Last Post: lukezo
  How to prepare a NumPy array which include float type array elements subhash 0 1,885 Mar-02-2020, 06:46 AM
Last Post: subhash
  How convert multidimensional array to two dimensional array tkkhan44 1 2,737 Feb-20-2019, 05:00 AM
Last Post: scidam
  How to add an element such as an average to a multi-dimensional array? xhughesey 6 3,927 Jan-06-2019, 10:47 PM
Last Post: xhughesey
  Checking the elements of a matrix with an elements of a list juniorcoder 11 5,758 Sep-17-2018, 03:02 PM
Last Post: gruntfutuk
  Three-dimensional Contour Plots minifizikus 1 3,246 Sep-13-2018, 10:56 PM
Last Post: Larz60+
  2 Dimensional NumPy for beginners Jack_Sparrow 2 3,067 May-08-2018, 05:21 PM
Last Post: killerrex
  Recurse through n-dimensional array ColdDeath 2 2,925 Apr-05-2018, 11:20 AM
Last Post: KenniT
  Two Dimensional Chart from CSV File srini1995 0 2,205 Nov-27-2017, 07:10 AM
Last Post: srini1995

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020