简体   繁体   English

使用Numpy和While循环生成所有可能的加法组合

[英]Using Numpy and while loops to generate all possible addition combinations

I am attempting to build an N-dimensional array from 2 base arrays that show all the possible combinations possible for the addition of all values such that: 我正在尝试从2个基本数组构建一个N维数组,这些基本数组显示所有可能值的加法组合:

Array A = [N0, N1, N2, ... , Nn]

Array B = [M0, M1, M2, ..., Mn]

Combinations = [[N0+M0, N0 + M1, ..., N0+Mn],

                [N1+M0, N1 + M1, ..., N1 + Mn],
                .
                .
                .,
                ]

I am aware that I can use the cartesian product to find all possible combinations (I do not care about repetition) however I do not need the product of all values, I need the addition. 我知道我可以使用笛卡尔积来找到所有可能的组合(我不在乎重复),但是我不需要所有值的乘积,我需要加法。 I cannot seem to get the while loop I've coded to be able to append the rows as it generates new data (code below). 我似乎无法获得我编码的while循环,因为它可以生成新数据时添加行(下面的代码)。

import numpy as np
MassFlowOx = np.linspace(0.1,103,150)*10**-3
MassFlowFuel = np.linspace(0.1,75,150)*10**-3
size = len(MassFlowFuel)
size_final=size**2
MassFlowComb = np.zeros(size)
temp = np.zeros(size, dtype=float)
i=0
j=0
MassFlowCombArray = np.zeros(size)

# runs all possible combinations for the addition of values in arrays
while i<size-1:
    temp[i] = MassFlowFuel[i] + MassFlowOx[i]
    while j<size-1:
        MassFlowOx[i] = MassFlowOx[j]
        print MassFlowOx[i]        
        j += 1
    MassFlowComb[i] = temp[i]

    MassFlowCombArray[i] = np.append([MassFlowComb[0]], [MassFlowComb[i]], axis = 0)
    i+=1

print MassFlowComb[i]

Is there a particular fucntion I can use? 我可以使用某种特殊功能吗? I have not been succesfull in identifying a method that does the addition of all combinations of values in a while loop as it gives sequence error. 我尚未成功地确定一种在while循环中进行值的所有组合加法的方法,因为它会产生序列错误。

EDIT: 编辑:

Both answers worked. 两种答案都有效。 The for loop makes it inN-dimensional and using np.add.outer makes an N dimensional array. for循环使其为N维,而使用np.add.outer N维数组。 For the purposes of this code the latter is more useful. 对于此代码而言,后者更为有用。

Thank you for all the responses! 感谢您的所有答复!

What you need is already implemented in NumPy and is called np.add.outer . 您所需的已经在NumPy中实现,称为np.add.outer If you want to take the difference, simply replace add by subtract . 如果您想获得差价,只需将add替换为subtract If you want to take the product, simply use outer . 如果要拿产品,只需使用outer For Nx1 and Mx1 arrays, the result will be of dimensions NxM . 对于Nx1Mx1阵列,结果将为NxM维。

Below is a simple example: 下面是一个简单的示例:

import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
np.add.outer(a, b)

array([[5, 6, 7], # [[1+4, 1+5, 1+6],
   [6, 7, 8],     #  [2+4, 2+5, 2+6],
   [7, 8, 9]])    #  [3+4, 3+5, 3+6]]

Why don't you just use double for loop? 您为什么不只使用double for循环?

import numpy as np
MassFlowOx = np.linspace(0.1,103,150)*10**-3
MassFlowFuel = np.linspace(0.1,75,150)*10**-3
MassFlowComb = []
for a in MassFlowOx:
    for b in MassFlowFuel:
        MassFlowComb.append(a+b)
MassFlowComb = np.array(MassFlowComb)

For this case is better to make a For-Loop than a While-Loop. 对于这种情况,创建循环比循环播放更好。 Your code could be as simples as: 您的代码可能很简单:

import numpy as np

MassFlowOx = np.linspace(0.1,103,150)*10**-3
MassFlowFuel = np.linspace(0.1,75,150)*10**-3

MassFlowComb = []

for  Fo in MassFlowOx:
  sub_list = []
  for  Ff in MassFlowFuel:
    sub_list.append(Fo+Ff)
  MassFlowComb.append(sub_list)

print(np.asarray(MassFlowComb))

Notice that I used a list to append the sums, but you can do it with numpy 请注意,我使用列表来追加总和,但是您可以使用numpy来完成

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

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