[Numpy-discussion] Efficiently defining a multidimensional array

Christopher Barker Chris.Barker@noaa....
Thu Aug 27 16:27:02 CDT 2009


Jonathan T wrote:
> I want to define a 3-D array as the sum of two 2-D arrays as follows:
> 
>    C[x,y,z] := A[x,y] + B[x,z]

Is this what you mean?

In [14]: A = np.arange(6).reshape((2,3,1))

In [15]: B = np.arange(12).reshape((1,3,4))


In [18]: A
Out[18]:
array([[[0],
         [1],
         [2]],

        [[3],
         [4],
         [5]]])

In [19]: B
Out[19]:
array([[[ 0,  1,  2,  3],
         [ 4,  5,  6,  7],
         [ 8,  9, 10, 11]]])

In [20]: A+B
Out[20]:
array([[[ 0,  1,  2,  3],
         [ 5,  6,  7,  8],
         [10, 11, 12, 13]],

        [[ 3,  4,  5,  6],
         [ 8,  9, 10, 11],
         [13, 14, 15, 16]]])


-Chris


-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker@noaa.gov


More information about the NumPy-Discussion mailing list