Suppose I have two 1D arrays (a and b) and I want to sum them element-wise to create a 2D array (c). The 2D array has dimension (n,m) where n is the length of a and m is the length of b. The precise relation goes as: c[i][j] = a[i]+b[j], where i runs from 0 to n-1 and j runs from 0 to m-1. For example, consider the following code
a = np.asarray([1,2,3])
b = np.asarray([1,2])
c = a+b
This code gives me broadcasting error. The goal is to get c = [[2,3],[3,4],[4,5]]. Obviously we can use a loop to get every element of c, but I am looking for a way to do this without going through a loop.
(a + b).shape == (3, 2), we require thata.shape == (3, 1)andb.shape == (1, 2).