From: Olivier B. <Oli...@id...> - 2005-04-14 14:19:43
|
Hello all, I'm converting some matlab scripts to matlibplot, and I don't know how to make some "slicing" efficently with matlibplot. What I want to do is to translate a matlab code like: x =3D [[01, 02, 03, 04, 05, 06]; [11, 12, 13, 14, 15, 16]; [21, 22, 23, 24, 25, 26]; [31, 32, 33, 34, 35, 36]; [41, 42, 43, 44, 45, 46]; [51, 52, 53, 54, 55, 56] ]; x(2:4,1:4) =20 ans =3D =20 11 12 13 14 21 22 23 24 31 32 33 34 =20 What I have done now in matplotlib is like: x =3D array ([[01, 02, 03, 04, 05, 06], [11, 12, 13, 14, 15, 16], [21, 22, 23, 24, 25, 26], [31, 32, 33, 34, 35, 36], [41, 42, 43, 44, 45, 46], [51, 52, 53, 54, 55, 56], ]) for i in range (3): for j in range (4): x2 [i][j] =3D x [i+1][j+0] so, x2 is now what I want: [[11,12,13,14,] [21,22,23,24,] [31,32,33,34,]] I will be very happy to make the "slicing" without the loop... I'm pretty new to matlibplot, so sorry if this is a too simple question. Thanks in advance for your help. Olivier --=20 . __ . ___ __. | Olivier Bornet Oli...@id... / / ` / / / / / | IDIAP http://www.idiap.ch/~bornet/ / / / / /--/ /--' | CP 592 http://www.idiap.ch/~bornet/pgp/ / /__.' / / / / | CH-1920 Martigny PGP-key: 0xC53D9218 |
From: Tim L. <ti...@cs...> - 2005-04-14 14:32:57
|
On Thu, 14 Apr 2005, Olivier Bornet <Oli...@id...> wrote... > Hello all, > > I'm converting some matlab scripts to matlibplot, and I don't know how > to make some "slicing" efficently with matlibplot. What I want to do is > to translate a matlab code like: You'll be pleased to know that the slicing works in almost exactly the same in way python. Try: x2 = x[1:4,0:4] The indexing is slightly different, but I'm sure you can work it out. Tim > > x = [[01, 02, 03, 04, 05, 06]; > [11, 12, 13, 14, 15, 16]; > [21, 22, 23, 24, 25, 26]; > [31, 32, 33, 34, 35, 36]; > [41, 42, 43, 44, 45, 46]; > [51, 52, 53, 54, 55, 56] > ]; > x(2:4,1:4) > > ans = > > 11 12 13 14 > 21 22 23 24 > 31 32 33 34 > > What I have done now in matplotlib is like: > > x = array ([[01, 02, 03, 04, 05, 06], > [11, 12, 13, 14, 15, 16], > [21, 22, 23, 24, 25, 26], > [31, 32, 33, 34, 35, 36], > [41, 42, 43, 44, 45, 46], > [51, 52, 53, 54, 55, 56], > ]) > for i in range (3): > for j in range (4): > x2 [i][j] = x [i+1][j+0] > > so, x2 is now what I want: > [[11,12,13,14,] > [21,22,23,24,] > [31,32,33,34,]] > > I will be very happy to make the "slicing" without the loop... > I'm pretty new to matlibplot, so sorry if this is a too simple question. > > Thanks in advance for your help. > > Olivier > -- > . __ . ___ __. | Olivier Bornet Oli...@id... > / / ` / / / / / | IDIAP http://www.idiap.ch/~bornet/ > / / / / /--/ /--' | CP 592 http://www.idiap.ch/~bornet/pgp/ > / /__.' / / / / | CH-1920 Martigny PGP-key: 0xC53D9218 `- |
From: Olivier B. <Oli...@id...> - 2005-04-14 14:44:28
|
Hi, On Fri, Apr 15, 2005 at 12:32:45AM +1000, Tim Leslie wrote: > You'll be pleased to know that the slicing works in almost exactly the > same in way python. Try: >=20 > x2 =3D x[1:4,0:4] Cool. :-D This is exactly what I'm searching for. But this is not really a Python syntax... The coma in the square brackets is not standard for lists. Maybe this come from the array type ? > The indexing is slightly different, but I'm sure you can work it out. Yes, no problem. Thanks for your help. Olivier --=20 . __ . ___ __. | Olivier Bornet Oli...@id... / / ` / / / / / | IDIAP http://www.idiap.ch/~bornet/ / / / / /--/ /--' | CP 592 http://www.idiap.ch/~bornet/pgp/ / /__.' / / / / | CH-1920 Martigny PGP-key: 0xC53D9218 |
From: John G. <jn...@eu...> - 2005-04-14 14:59:02
|
Lists are 1-dimensional, so the comma doesn't really make sense. You can, however, slice lists in python eg >>> x = range(100) >>> x[10:100:5] [10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95] You're right, that the commas come into play when you are dealing with the numarray (or numeric) arrays, where you may well have more than one dimension. It is a natural extension of the python slice syntax to multiple dimensions. John Olivier Bornet wrote: >Hi, > >On Fri, Apr 15, 2005 at 12:32:45AM +1000, Tim Leslie wrote: > > >>You'll be pleased to know that the slicing works in almost exactly the >>same in way python. Try: >> >>x2 = x[1:4,0:4] >> >> > >Cool. :-D >This is exactly what I'm searching for. But this is not really a Python >syntax... The coma in the square brackets is not standard for lists. >Maybe this come from the array type ? > > > >>The indexing is slightly different, but I'm sure you can work it out. >> >> > >Yes, no problem. > >Thanks for your help. > > Olivier > > |
From: John H. <jdh...@ac...> - 2005-04-14 15:16:07
|
>>>>> "Olivier" == Olivier Bornet <Oli...@id...> writes: Olivier> Cool. :-D This is exactly what I'm searching for. But Olivier> this is not really a Python syntax... The coma in the Olivier> square brackets is not standard for lists. Maybe this Olivier> come from the array type ? Yes, this is Numeric/numarray slicing, not list slicing. When you get some time, you should read the pdf Numeric or numarray documentation. It's comprehensive and very good. Numarray also offers enhanced indexing ( Numeric does not yet) which is a convenience you probably expect coming from matlab. Eg, you can index an array with a sequence of integers (you must be careful that the sequence is not a tuple however; lists or arrays of ints are ok) >>> import numarray as na >>> a = na.arange(0.0, 2.0, 0.1) >>> ind = [2,5,7] >>> a[ind] array([ 0.2, 0.5, 0.7]) JDH |
From: Olivier B. <Oli...@id...> - 2005-04-15 07:57:41
|
Hello all, > Yes, this is Numeric/numarray slicing, not list slicing. When you get > some time, you should read the pdf Numeric or numarray documentation. > It's comprehensive and very good. Thanks for all the answers. I'm now looking at the numarray/numeric documentation as suggested. Good day, and thanks again. Olivier --=20 . __ . ___ __. | Olivier Bornet Oli...@id... / / ` / / / / / | IDIAP http://www.idiap.ch/~bornet/ / / / / /--/ /--' | CP 592 http://www.idiap.ch/~bornet/pgp/ / /__.' / / / / | CH-1920 Martigny PGP-key: 0xC53D9218 |
From: Chris B. <Chr...@no...> - 2005-04-14 16:17:33
|
Olivier Bornet wrote: > This is exactly what I'm searching for. But this is not really a Python > syntax... The coma in the square brackets is not standard for lists. > Maybe this come from the array type ? Yes, it does. I you are doing anything MATLAB-y with Python, you really want to know about either numarray or Numeric. numarray is a little bot more like MATLAB (it allows array indexing), and Numeric has better performance with small arrays, but either will do for most uses. It looks like we're on the way to a grand unification of the two anyway. Make sure you go find the docs f0or the package you choose, and read through them, I think you'll like them a lot! By the way: aside from the looping, when you do: x = A[i][j] rather than: x = A[i,j] there is a performance hit because the first version is creating a new array out of A[i], then indexing into that, rather than just indexing directly into the 2-d (or more d) array. This is particularly pronounced with numarray, as the array creation overhead is larger than with Numeric. -Chris -- Christopher Barker, Ph.D. Oceanographer NOAA/OR&R/HAZMAT (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chr...@no... |