[Numpy-discussion] generic slicing question

Sturla Molden sturla@molden...
Fri Mar 4 08:24:02 CST 2011


Den 04.03.2011 15:19, skrev Neal Becker:
> This will subtract col 0 from all cols of a 2-d array:
>
> # subtract col 0 from all cols
> def ll2llr (m):
>      return m - m[:,0][:,np.newaxis]
>
>
> In general, how to subtract the (n-1)d slice at index 0 from all elements of an
> n-d array?
>
> (I think a 3d version is:
> m - m[:,:,0][:,:,np.newaxis]
> )

m - m[...,0][...,np.newaxis]

Sturla


More information about the NumPy-Discussion mailing list