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

John J. Lee phrxy at csv.warwick.ac.uk
Mon Mar 19 17:58:30 EST 2001

On Mon, 19 Mar 2001, Aureli Soria Frisch wrote:
> 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.
> Is there any more elegant solution? The key point is: how to operate a N-D
> array with a 1D in one determined axis?

I'm not sure I understand exactly what you want.  Are you sure you don't
want to do A[n1,n2,...,:,...,nN] instead?  (the '...'s are just an
indication of where the extra numbers would go, not the literal Numeric
syntax. If so, you can use

s = slice(None)
# n is your list [n1, n2, n3, ..., n(k-1), n(k+1), ...,nN]
n.insert(k, s)
print "answer is", A[n] - B
print slice.__doc__

(possibly there is an off-by-one error in the above, but you get the idea)


More information about the NumPy-Discussion mailing list