[Numpy-discussion] addressing a submatrix

Karol Langner karol.langner at kn.pl
Wed Jun 14 04:50:33 CDT 2006


On Wednesday 14 June 2006 11:14, Martin Wiechert wrote:
> is there a concise way to address a subrectangle of a 2d array? So far I'm
> using
>
> A [I] [:, J]
>
> which is not pretty and more importantly only works for reading the
> subrectangle. Writing does *not* work. (Cf. session below.)
>
> Any help would be appreciated.
>
> Thanks,
> Martin

You can also use A[m:n,r:s] to refernce a subarray.

For instance:
>>> a = numpy.zeros((5,5))
>>> b = numpy.ones((3,3))
>>> a[1:4,1:4] = b
>>> a
array([[0, 0, 0, 0, 0],
       [0, 1, 1, 1, 0],
       [0, 1, 1, 1, 0],
       [0, 1, 1, 1, 0],
       [0, 0, 0, 0, 0]])

Cheers,
Karol

-- 
written by Karol Langner
śro cze 14 11:49:35 CEST 2006




More information about the Numpy-discussion mailing list