On Wednesday 14 June 2006 11:14, Martin Wiechert wrote: > 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
You can achieve this by using the "take" function twice, in this fashion: >>> a = numpay.ones((10,10)) >>> for i in range(5): ... for j in range(5): ... a[i][j] = i+j ... >>> a array([[0, 1, 2, 3, 4], [1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7], [4, 5, 6, 7, 8]]) >>> print a.take.__doc__ a.take(indices, axis=None). Selects the elements in indices from array a along the given axis. >>> a.take((1,2,3),axis=0) array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7]]) >>> a.take((1,2,3),axis=0).take((2,3),axis=1) array([[3, 4], [4, 5], [5, 6]]) Cheers, Karol -- written by Karol Langner śro cze 14 11:27:33 CEST 2006 _______________________________________________ Numpy-discussion mailing list Numpy-discussion@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/numpy-discussion