[Numpy-discussion] How operating arrays in a determined axis?

Aureli Soria Frisch Aureli.Soria_Frisch at ipk.fhg.de
Mon Mar 19 14:59:29 EST 2001


Hello!

I have a N-dimensional array A and I want to operate in one of the axis (k)
with a 1 dimensional array (for instance,  subtracting an array B of length
k). I have looked for some solutions in the manual and did not found any.

So after testing a lot I found following solution:

A=Numeric.array(a)

A.shape=(a1,a2,...,ak,...,aN)	#where N is any int value

B=Numeric.array(b)
B.shape=(ak,)


A_modified=Numeric.reshape(A, (a1*a2*...*aN,ak)) #ak is not included in the
product

result=[]
for i in A_modified:
	result.append(i - B)


and finally reshaping the result appropriately. But it does not seem
neither elegant nor simple.

Is there any more elegant solution? The key point is: how to operate a N-D
array with a 1D in one determined axis?

Thanks in advance.
Regards,
Aureli




#################################
Aureli Soria Frisch
Fraunhofer IPK
Dept. Pattern Recognition

post: Pascalstr. 8-9, 10587 Berlin, Germany
e-mail:aureli at ipk.fhg.de
fon: +49 30 39 00 61 50
fax: +49 30 39 17 517
#################################






More information about the NumPy-Discussion mailing list