[Numpy-discussion] Selected and altering a submatrix
seb.haase at gmx.net
Mon Jan 29 23:44:40 EST 2007
mat[0:3, 4:7] += 1
On 1/29/07, Steve Lianoglou <lists.steve at arachnedesign.net> wrote:
> I was just curious what the "correct" (fast) way to select and alter
> a submatrix.
> For example, say I have a 10x10 array and only want to add some
> number to the elements in the submatrix that consists of the [0,1,2]
> th rows, and [4,5,6]th colums.
> You can imagine that those rows/cols select a square in the top-
> middle of the 10x10 which I want to alter.
> The only way I can get this to work is if I iterate over the indices
> in one of the dimensions (say the rows) and use the column indices to
> slice out the relevant elements to add to .. is there a NumPy-thonic
> way to do this:
> import numpy as N
> mat = N.zeros((10,10))
> rows = [0,1,2]
> cols = [4,5,6]
> for row in rows:
> mat[row,cols] += 1
> I found something on the lists from a few years back that was in
> reference to numeric or numarray that suggested doing some gymnastics
> with take/put, but it still seemed as if there was no way to slice
> out this view of a matrix w/o making a copy.
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
More information about the NumPy-Discussion