first and last index as in matlab

Discussion in 'Python' started by Evan, Dec 17, 2006.

  1. Evan

    Evan Guest

    In matlab I can do the following:

    >> ind = [3,5,7,2,4,7,8,24]

    ind = 3 5 7 2 4 7 8 24
    >> ind(1) ans = 3
    >> ind(end) ans = 24
    >> ind([1 end]) ans = 3 24


    but I can't get the last line in python:

    In [690]: ind = [3,5,7,2,4,7,8,24]
    In [691]: ind[0] Out[691]: 3
    In [692]: ind[-1:] Out[692]: [24]
    In [693]: ??

    How do I pull out multiple indices as in matlab?


    Thanks, Evan
     
    Evan, Dec 17, 2006
    #1
    1. Advertising

  2. Evan wrote in news: in
    comp.lang.python:

    > In matlab I can do the following:
    >
    >>> ind = [3,5,7,2,4,7,8,24]

    > ind = 3 5 7 2 4 7 8 24
    >>> ind(1) ans = 3
    >>> ind(end) ans = 24
    >>> ind([1 end]) ans = 3 24

    >
    > but I can't get the last line in python:
    >
    > In [690]: ind = [3,5,7,2,4,7,8,24]
    > In [691]: ind[0] Out[691]: 3
    > In [692]: ind[-1:] Out[692]: [24]
    > In [693]: ??
    >
    > How do I pull out multiple indices as in matlab?


    [ind[0], ind[-1]]

    or if you need something that can be generalised:

    [ind for i in [0, -1]]

    so if you have:

    indexes_of_ind = [0, 2, -1, -2]

    you can write:

    [ind for i in indexes_of_ind]


    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Dec 17, 2006
    #2
    1. Advertising

  3. Evan

    Paul McGuire Guest

    "Evan" <> wrote in message
    news:...
    > In matlab I can do the following:
    >
    >>> ind = [3,5,7,2,4,7,8,24]

    > ind = 3 5 7 2 4 7 8 24
    >>> ind(1) ans = 3
    >>> ind(end) ans = 24
    >>> ind([1 end]) ans = 3 24

    >
    > but I can't get the last line in python:
    >
    > In [690]: ind = [3,5,7,2,4,7,8,24]
    > In [691]: ind[0] Out[691]: 3
    > In [692]: ind[-1:] Out[692]: [24]
    > In [693]: ??
    >
    > How do I pull out multiple indices as in matlab?
    >
    >
    > Thanks, Evan
    >


    Or use the third element of a slice, which defines a stepsize, and pick a
    step that will go from the first to the last element:

    >>> lst = list("ABCDEFG")
    >>> lst

    ['A', 'B', 'C', 'D', 'E', 'F', 'G']
    >>> lst[0::len(lst)-1]

    ['A', 'G']

    -- Paul
     
    Paul McGuire, Dec 17, 2006
    #3
  4. Evan

    Beliavsky Guest

    Evan wrote:
    > In matlab I can do the following:
    >
    > >> ind = [3,5,7,2,4,7,8,24]

    > ind = 3 5 7 2 4 7 8 24
    > >> ind(1) ans = 3
    > >> ind(end) ans = 24
    > >> ind([1 end]) ans = 3 24

    >
    > but I can't get the last line in python:
    >
    > In [690]: ind = [3,5,7,2,4,7,8,24]
    > In [691]: ind[0] Out[691]: 3
    > In [692]: ind[-1:] Out[692]: [24]
    > In [693]: ??
    >
    > How do I pull out multiple indices as in matlab?


    If you want functionality similar to Matlab in Python, you should use
    Numpy, which has the "take" function to do what you want.
     
    Beliavsky, Dec 17, 2006
    #4
  5. Evan

    Robert Kern Guest

    Beliavsky wrote:
    > Evan wrote:
    >> In matlab I can do the following:
    >>
    >>>> ind = [3,5,7,2,4,7,8,24]

    >> ind = 3 5 7 2 4 7 8 24
    >>>> ind(1) ans = 3
    >>>> ind(end) ans = 24
    >>>> ind([1 end]) ans = 3 24

    >> but I can't get the last line in python:
    >>
    >> In [690]: ind = [3,5,7,2,4,7,8,24]
    >> In [691]: ind[0] Out[691]: 3
    >> In [692]: ind[-1:] Out[692]: [24]
    >> In [693]: ??
    >>
    >> How do I pull out multiple indices as in matlab?

    >
    > If you want functionality similar to Matlab in Python, you should use
    > Numpy, which has the "take" function to do what you want.


    Actually, in numpy, we also have "fancy indexing" similar to Matlab's:


    In [1]: from numpy import *

    In [2]: ind = array([3,5,7,2,4,7,8,24])

    In [3]: ind[[0, -1]]
    Out[3]: array([ 3, 24])


    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
     
    Robert Kern, Dec 17, 2006
    #5
  6. Evan

    sturlamolden Guest

    It's quite straight forward, actually. What you need to know is that -1
    is the index of the last element in a sequence, and that slicing
    excludes the 'end' value in 'start:end'. So if you type arr[0:N], you
    get the subsequence

    [arr[0], arr[1], arr[2], ..., arr[N-1]]

    When comparing with Matlab, Python slicing works like this:

    arr(1:end) -> arr[:] or arr[0:]
    arr(1:end-1) -> arr[:-1] or arr[0:-1]
    arr(1:end-N) -> arr[:-N] or arr[0:-N]
    arr(end) -> arr[-1]
    arr(1) -> arr[0]
    arr(1:2:end) -> arr[::2] or arr[0::2]
    arr(1:2:end-1) -> arr[:-1:2] or arr[0:-1:2]

    Python slicing is not completely like Matlab, because it was adoped
    from Haskell. It can do the same as Matlab's indexing, but the syntax
    is different. If you think Matlab's indexing is more intuitive it is
    just because you are more used to it.
     
    sturlamolden, Dec 17, 2006
    #6
  7. Evan

    Evan Guest

    Thanks for all the replies, it's much clearer now.

    -Evan
     
    Evan, Dec 18, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. siki
    Replies:
    0
    Views:
    1,003
  2. Luna Moon
    Replies:
    16
    Views:
    1,426
    Giovanni Gherdovich
    Aug 8, 2008
  3. Shawn W_
    Replies:
    5
    Views:
    319
    Aldric Giacomoni
    Sep 16, 2009
  4. Replies:
    7
    Views:
    385
    John W. Krahn
    Nov 13, 2007
  5. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    356
    Tomasz Chmielewski
    Mar 4, 2008
Loading...

Share This Page