[Numpy-discussion] addressing a submatrix

Martin Wiechert martin.wiechert at gmx.de
Wed Jun 14 04:14:17 CDT 2006


Hi list,

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



In [1]:a = zeros ((4,4))

In [2]:b = ones ((2,2))

In [3]:c = array ((1,2))

In [4]:a [c] [:, c] = b

In [5]:a
Out[5]:
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]])

In [6]:a [:, c] [c] = b

In [7]:a
Out[7]:
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]])

In [8]:a [c, c] = b

In [9]:a
Out[9]:
array([[0, 0, 0, 0],
       [0, 1, 0, 0],
       [0, 0, 1, 0],
       [0, 0, 0, 0]])

In [10]:a [c] [:, c]
Out[10]:
array([[1, 0],
       [0, 1]])

In [11]:





More information about the Numpy-discussion mailing list