getting slices out of an array

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 = [[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 Olivier.Bornet@...567...
  / / ` / / / / / | IDIAP http://www.idiap.ch/~bornet/
/ / / / /--/ /--' | CP 592 http://www.idiap.ch/~bornet/pgp/
/ /__.' / / / / | CH-1920 Martigny PGP-key: 0xC53D9218

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 Olivier.Bornet@...567...
  / / ` / / / / / | IDIAP Olivier Bornet @ Idiap
/ / / / /--/ /--' | CP 592 Olivier Bornet PGP key
/ /__.' / / / / | CH-1920 Martigny PGP-key: 0xC53D9218

`-

···

On Thu, 14 Apr 2005, Olivier Bornet <Olivier.Bornet@...567...> wrote...

Hi,

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. :smiley:
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

···

On Fri, Apr 15, 2005 at 12:32:45AM +1000, Tim Leslie wrote:
--
   . __ . ___ __. | Olivier Bornet Olivier.Bornet@...567...
  / / ` / / / / / | IDIAP Olivier Bornet @ Idiap
/ / / / /--/ /--' | CP 592 Olivier Bornet PGP key
/ /__.' / / / / | CH-1920 Martigny PGP-key: 0xC53D9218

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

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

Chris.Barker@...259...