I would like to add two numpy arrays of different shapes, but without translation, and the “missing” values are treated as zeros. Probably the easiest way with an example like
[1, 2, 3] + [2] -> [3, 2, 3]
or
[1, 2, 3] + [[2], [1]] -> [[3, 2, 3], [1, 0, 0]]
I do not know the form in advance.
I fiddled with np.shape for everyone, trying to find the smallest figure that contains both of them, inserting each into a zero-ed array of this shape, and then adding them. But this seems to be pretty much work, is there an easier way?
Thanks in advance!
edit: “A lot of work” I meant “a lot of work for me” and not for the car, I am looking for elegance, not efficiency: my effort, getting the smallest form, holding them both,
def pad(a, b) :
sa, sb = map(np.shape, [a, b])
N = np.max([len(sa),len(sb)])
sap, sbp = map(lambda x : x + (1,)*(N-len(x)), [sa, sb])
sp = np.amax( np.array([ tuple(sap), tuple(sbp) ]), 1)
Not really: -/