Numerical Python array math with different sized arrays

Emile van Sebille emile at fenx.com
Mon Jun 24 23:52:56 EDT 2002


Michael Weiss
> I'm using Numeric Python's arrays. I want to perform the various math
> operators between arrays (+-*/ etc...) .
> While all my arrays are 1 dimensional, they may be different lengths.
The
> result I'd like in a mis-matched size case is the following:
> >>> a = Numeric.array([1,2,3,4])
> >>> b = Numeric.array([1,2])
> >>> print a+b
> [2,4,3,4]
>

Looks like these work.

For two Numeric arrays, if you want a op b to return the full length of
a:

add:
a[:len(b)] = map(Numeric.sum, zip(a,b))

subtract:
a[:len(b)] = map(Numeric.sum, zip(a,b*-1))

multiply:
a[:len(b)] = map(Numeric.product, zip(a,b))

divide:
a[:len(b)]=Numeric.divide(a[:len(b)],b)


HTH,

--

Emile van Sebille
emile at fenx.com

---------




More information about the Python-list mailing list