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 |