From: Martin W. <mar...@gm...> - 2006-06-14 09:17:12
|
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]: |
From: Simon B. <si...@ar...> - 2006-06-14 09:27:43
|
On Wed, 14 Jun 2006 11:14:17 +0200 Martin Wiechert <mar...@gm...> wrote: > > Hi list, > > is there a concise way to address a subrectangle of a 2d array? So far I'm > using > > A [I] [:, J] what about A[I,J] ? Simon. >>> import numpy >>> a=numpy.zer numpy.zeros numpy.zeros_like >>> a=numpy.zeros([4,4]) >>> a array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) >>> a[2:3,2:3]=1 >>> a array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0]]) >>> a[1:3,1:3]=1 >>> a array([[0, 0, 0, 0], [0, 1, 1, 0], [0, 1, 1, 0], [0, 0, 0, 0]]) >>> -- Simon Burton, B.Sc. Licensed PO Box 8066 ANU Canberra 2601 Australia Ph. 61 02 6249 6940 http://arrowtheory.com |
From: Karol L. <kar...@kn...> - 2006-06-14 09:31:52
|
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 =3D numpay.ones((10,10)) >>> for i in range(5): =2E.. for j in range(5): =2E.. a[i][j] =3D i+j =2E..=20 >>> 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=3DNone). Selects the elements in indices from array a= =20 along the given axis. >>> a.take((1,2,3),axis=3D0) array([[1, 2, 3, 4, 5], [2, 3, 4, 5, 6], [3, 4, 5, 6, 7]]) >>> a.take((1,2,3),axis=3D0).take((2,3),axis=3D1) array([[3, 4], [4, 5], [5, 6]]) Cheers, Karol =2D-=20 written by Karol Langner =B6ro cze 14 11:27:33 CEST 2006 |
From: Martin W. <Mar...@mp...> - 2006-06-14 09:36:33
|
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. Martin On Wednesday 14 June 2006 20:25, Simon Burton wrote: > On Wed, 14 Jun 2006 11:14:17 +0200 > > Martin Wiechert <mar...@gm...> wrote: > > Hi list, > > > > is there a concise way to address a subrectangle of a 2d array? So far > > I'm using > > > > A [I] [:, J] > > what about A[I,J] ? > > Simon. > > >>> import numpy > >>> a=numpy.zer > > numpy.zeros numpy.zeros_like > > >>> a=numpy.zeros([4,4]) > >>> a > > array([[0, 0, 0, 0], > [0, 0, 0, 0], > [0, 0, 0, 0], > [0, 0, 0, 0]]) > > >>> a[2:3,2:3]=1 > >>> a > > array([[0, 0, 0, 0], > [0, 0, 0, 0], > [0, 0, 1, 0], > [0, 0, 0, 0]]) > > >>> a[1:3,1:3]=1 > >>> a > > array([[0, 0, 0, 0], > [0, 1, 1, 0], > [0, 1, 1, 0], > [0, 0, 0, 0]]) |
From: Karol L. <kar...@kn...> - 2006-06-14 09:50:44
|
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. =46or instance: >>> a =3D numpy.zeros((5,5)) >>> b =3D numpy.ones((3,3)) >>> a[1:4,1:4] =3D 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 =2D-=20 written by Karol Langner =B6ro cze 14 11:49:35 CEST 2006 |
From: Pau G. <pau...@gm...> - 2006-06-14 10:02:08
|
On 6/14/06, Martin Wiechert <Mar...@mp...> 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 |
From: Martin W. <mar...@gm...> - 2006-06-14 14:22:27
|
Thanks Pau, that's exactly what I was looking for. Martin On Wednesday 14 June 2006 12:01, you wrote: > On 6/14/06, Martin Wiechert <Mar...@mp...> 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 |