How To Add Numpy Arrays Of Different Shape
I am solving an Optimization problem, and after differentiation the equation becomes y + mu + b_i + c_j + (U.t,v) where shape of the variables are as follows: len(y) = 8992 mu = 2
Solution 1:
You can't add arrays of different dimensions elementwise, obviously, because it is not well defined.
What you can do though, is pad the smaller arrays with 0
s to match the largest array, by:
def pad_with_zeros(b_00, b_11):
b_max, b_min = (b_00, b_11) if b_00.shape[0] > b_11.shape[0] else (b_11, b_00)
b_min_z = np.zeros_like(b_max)
b_min_z[:b_min.shape[0],] = b_min
return b_max, b_min_z
b_00 = np.array([2.2, 1.1, 4.4])
b_11 = np.array([1.2, 3.3])
b_max, b_min_z = pad_with_zeros(b_00, b_11)
b_max, b_min_z
and add them together when they are all of the same shape, the only question is if it is what you need?
Cheers.
Post a Comment for "How To Add Numpy Arrays Of Different Shape"