[Numpy-discussion] addressing a submatrix

Pau Gargallo pau.gargallo at gmail.com
Wed Jun 14 05:02:06 CDT 2006


On 6/14/06, Martin Wiechert <Martin.Wiechert at mpimf-heidelberg.mpg.de> wrote:
> 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.

you can use A[ ix_(I,J) ] to do what you want.

But, if you just want subrectangular regions then A[1:4,1:4] is enough.
Please note that A[1:4,1:4] is not the same as A[ arange(1,4), arange(1,4) ],
but is the same as A[ ix_(arange(1,4), arange(1,4)) ].

hope this heps
pau




More information about the Numpy-discussion mailing list