简体   繁体   中英

How to add an array to each row in a 2D array?

I want to do elements-wise addition of two arrays. The first one is 1D and is contained in a dictionary under the key "cras", the second one is 2D. Example:

   OrderedDict([('head', array([ 2,  0, 20], dtype=int32)), ('valid', '1  # 
   volume info valid'), ('filename', '../mri/filled-pretess255.mgz'), 
   ('volume', array([256, 256, 256])), ('voxelsize', array([1., 1., 1.])), 
   ('xras', array([-1.,  0.,  0.])), ('yras', array([ 0.,  0., -1.])), 
    ('zras', array([0., 1., 0.])), ('cras', array([-3.433, 18.419, 28.598]))])

    'cras', array([-3.433, 18.419, 28.598])

add to every element of

[[ -9.022 -81.936  -1.822]
 [ -9.554 -82.001  -1.929]
 [-10.186 -81.872  -1.779]
 ...
 [-16.673  76.043 -18.319]
 [-16.989  76.213 -17.823]
 [-17.568  75.554 -18.709]]

result is expected:

[-3.433+(-9.022), 18.419+(-81.936), 28.598+(-1.822)]
....

How to implement this?

If your data is very large, you may want to look into the numpy module. If not, you can do this with zip and a list comprehension:

matrix = [[ -9.022, -81.936,  -1.822],
          [ -9.554, -82.001,  -1.929],
          [-10.186, -81.872,  -1.779],
          [-16.673,  76.043, -18.319],
          [-16.989,  76.213, -17.823],
          [-17.568,  75.554, -18.709]]

cras   = [-3.433, 18.419, 28.598]

result = [ [ a+b for a,b in zip(cras,line) ] for line in matrix ]

for line in result: print(line)

# (rounded and aligned the printed values for legibility)
[-12.455, -63.517, 26.776]
[-12.987, -63.582, 26.669]
[-13.619, -63.453, 26.819]
[-20.106,  94.462, 10.279]
[-20.422,  94.632, 10.775]
[-21.001,  93.973,  9.889]

You are trying to add the contents of

cras

to every element of the sample 2D list that you have provided if I am understanding correctly. What I did is convert your sample list into a numpy array and then performed element-wise addition using numpy again.

from collections import OrderedDict
from numpy import array, int32, asarray, add

def adding():
    dict = OrderedDict([('head', array([2, 0, 20], dtype=int32)), ('valid', '1  # \
                 volume info valid'), ('filename', '.. / mri / filled - pretess255.mgz'),
                 ('volume', array([256, 256, 256])), ('voxelsize', array([1., 1., 1.])),
                 ('xras', array([-1., 0., 0.])), ('yras', array([0., 0., -1.])),
                 ('zras', array([0., 1., 0.])), ('cras', array([-3.433, 18.419, 28.598]))])

    'cras', array([-3.433, 18.419, 28.598])

    sample_arr = asarray([[-9.022, -81.936, -1.822],
                 [-9.554, -82.001, -1.929],
                 [-10.186, -81.872, -1.779],
                 [-16.673,  76.043, -18.319],
                 [-16.989, 76.213, -17.823],
                 [-17.568,  75.554, -18.709]])

    result = add(dict['cras'], sample_arr)
    print(result)
    print([-3.433+(-9.022), 18.419+(-81.936), 28.598+(-1.822)])

Result:

[[-12.455 -63.517  26.776]
 [-12.987 -63.582  26.669]
 [-13.619 -63.453  26.819]
 [-20.106  94.462  10.279]
 [-20.422  94.632  10.775]
 [-21.001  93.973   9.889]]

[-12.455, -63.51700000000001, 26.776]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM