[SciPy-user] addition of vectors of different lengths

Dan Christensen jdc at uwo.ca
Tue Aug 2 21:27:26 CDT 2005


I've got some 1d numarrays that I'd like to add componentwise, but
sometimes they differ in length.  When this in the case, I'd like to
imagine that the shorter one is extended by zeros.  Is there a fast
way to do this sort of addition?  My current solution is slow,
probably because of the use of concatenate:

        l1 = len(v1)
        l2 = len(v2)
        if l1 == l2:
            v = v1+v2
        elif l1 < l2:
            v = concatenate((v1+v2[:l1], v2[l1:]))
        else:
            v = concatenate((v1[:l2]+v2, v1[l2:]))

The vectors are short, typically just 10 or 20 elements, at most 256.

Thanks,

Dan



More information about the SciPy-user mailing list