[Numpy-discussion] addressing a submatrix
Martin Wiechert
Martin.Wiechert at mpimf-heidelberg.mpg.de
Wed Jun 14 04:33:45 CDT 2006
Hi Simon,
thanks for your reply.
A [I, J]
seems to only work if the indices are *strides* as in your example. I need
fancy indices (like I = (1,3,4), J = (0,3,5)), and for them A [I, J] won't do
what I want. As you can see from the example session I posted it does not
address the whole rectangle IxJ but only the elements (I_1, J_1), (I_2, J_2).
E.g., if I==J this is the diagonal of the submatrix, not the full submatrix.
Martin
On Wednesday 14 June 2006 20:25, Simon Burton wrote:
> On Wed, 14 Jun 2006 11:14:17 +0200
>
> Martin Wiechert <martin.wiechert at gmx.de> wrote:
> > Hi list,
> >
> > is there a concise way to address a subrectangle of a 2d array? So far
> > I'm using
> >
> > A [I] [:, J]
>
> what about A[I,J] ?
>
> Simon.
>
> >>> import numpy
> >>> a=numpy.zer
>
> numpy.zeros numpy.zeros_like
>
> >>> a=numpy.zeros([4,4])
> >>> a
>
> array([[0, 0, 0, 0],
> [0, 0, 0, 0],
> [0, 0, 0, 0],
> [0, 0, 0, 0]])
>
> >>> a[2:3,2:3]=1
> >>> a
>
> array([[0, 0, 0, 0],
> [0, 0, 0, 0],
> [0, 0, 1, 0],
> [0, 0, 0, 0]])
>
> >>> a[1:3,1:3]=1
> >>> a
>
> array([[0, 0, 0, 0],
> [0, 1, 1, 0],
> [0, 1, 1, 0],
> [0, 0, 0, 0]])
More information about the Numpy-discussion
mailing list