简体   繁体   English

如何在2D数组的每一行中添加数组?

[英]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. 第一个是1D,包含在键“ cras”下的字典中,第二个是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. 如果您的数据非常大,则可能需要研究numpy模块。 If not, you can do this with zip and a list comprehension: 如果没有,您可以使用zip和列表理解功能来做到这一点:

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 cras

to every element of the sample 2D list that you have provided if I am understanding correctly. 如果我理解正确的话,请访问您提供的示例2D列表的每个元素。 What I did is convert your sample list into a numpy array and then performed element-wise addition using numpy again. 我所做的是将您的示例列表转换为numpy数组,然后再次使用numpy执行逐元素加法。

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何向 3d 数组的每个 2d 数组添加(不附加)一个 2d 数组? - How to add (not append) a 2d array to each 2d array of a 3d array? 如何从二维数组中返回每个元素的行索引? - How to return the row index of each element from a 2D array? 如何将 Pandas DF 中的每一行转换为二维 numpy 数组? - How to convert each row in Pandas DF to 2D numpy array? 如何通过比较每一行在二维数组中找到不同的数字? - How to find the different number in a 2D array by comparing each row? 如何使用二维数组每一行的元素压缩一维数组中的每个元素? - How to zip each element from a 1D array with the elements from each row of a 2D array? 如何按另一个 2D 数组对 3D numpy 数组的每一行进行排序? - How to sort each row of a 3D numpy array by another 2D array? Numpy:如何将单个数组堆叠成更大数组的每一行并将其变成二维数组? - Numpy: How to stack a single array into each row of a bigger array and turn it into a 2D array? 2D 到 1D numpy 数组,每行的列索引 - 2D to 1D numpy array with indices of column for each row 对于2d numpy数组的每一行,在第二个2d数组中获取相等行的索引 - For each row of a 2d numpy array get the index of an equal row in a second 2d array 如何通过另一个 2d 索引数组对 2d numpy-array 的每一行进行排序? - How can I sort each row of the 2d numpy-array by another 2d index-array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM