problems with duplicating and slicing an array

Discussion in 'Python' started by Yun Mao, Jan 21, 2005.

  1. Yun Mao

    Yun Mao Guest

    Hi python gurus,
    I have some questions when I'm using python numeric:

    1. When I do v = u[:, :], it seems u and v still point to the same
    memory. e.g. When I do v[1,1]=0, u[1,1] will be zero out as well.
    What's the right way to duplicate an array? Now I have to do v =
    dot(u, identity(N)), which is kind of silly.

    2. Is there a way to do Matlab style slicing? e.g. if I have
    i = array([0, 2])
    x = array([1.1, 2.2, 3.3, 4.4])
    I wish y = x(i) would give me [1.1, 3.3]
    Now I'm using map, but it gets a little annoying when there are two
    dimensions.
    Any ideas would be deeply appreciated!

    Thanks!!!

    -Y
     
    Yun Mao, Jan 21, 2005
    #1
    1. Advertising

  2. Yun Mao wrote:
    > Hi python gurus,
    > I have some questions when I'm using python numeric:
    >
    > 1. When I do v = u[:, :], it seems u and v still point to the same
    > memory. e.g. When I do v[1,1]=0, u[1,1] will be zero out as well.
    > What's the right way to duplicate an array? Now I have to do v =
    > dot(u, identity(N)), which is kind of silly.


    Hmm... I don't know Numeric, but in numarray (Numeric's replacement),
    you just call the copy method:

    py> u = na.arange(6, shape=(2, 3))
    py> v = u.copy()
    py> v[0,0] = 100
    py> u
    array([[0, 1, 2],
    [3, 4, 5]])
    py> v
    array([[100, 1, 2],
    [ 3, 4, 5]])

    > 2. Is there a way to do Matlab style slicing? e.g. if I have
    > i = array([0, 2])
    > x = array([1.1, 2.2, 3.3, 4.4])
    > I wish y = x(i) would give me [1.1, 3.3]


    This also works exactly as you'd hope in numarray.

    py> i = na.array([0, 2])
    py> x = na.array([1.1, 2.2, 3.3, 4.4])
    py> x
    array([ 1.1, 3.3])

    Do you have any reason that you have to use Numeric? Or can you upgrade
    to numarray?

    Steve
     
    Steven Bethard, Jan 21, 2005
    #2
    1. Advertising

  3. Yun Mao

    Guest

    Yun Mao wrote:

    >I have some questions when I'm using python numeric:


    >1. When I do v = u[:, :], it seems u and v still point to the same
    >memory. e.g. When I do v[1,1]=0, u[1,1] will be zero out as well.
    >What's the right way to duplicate an array? Now I have to do v =
    >dot(u, identity(N)), which is kind of silly.


    You can use v = 1*u or v = u.copy() to get the type of copy you want.

    >2. Is there a way to do Matlab style slicing?


    Use the take function.

    The output of the following code

    from Numeric import array,take
    a = array([1,2])
    b = a
    c = 1*a
    d = a.copy()
    a[0] = 3
    print b
    print c
    print d
    print take(a,[1,0,1])

    is

    [3 2]
    [1 2]
    [1 2]
    [2 3 2]
     
    , Jan 21, 2005
    #3
    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. Tero Kapanen

    Slicing of an array: wrong direction

    Tero Kapanen, Nov 4, 2003, in forum: VHDL
    Replies:
    11
    Views:
    7,293
    Allan Herriman
    Nov 7, 2003
  2. Yun Mao
    Replies:
    3
    Views:
    355
    David Isaac
    Jan 23, 2005
  3. jeg

    2d array slicing problem

    jeg, Oct 7, 2005, in forum: Python
    Replies:
    4
    Views:
    398
    Robert Kern
    Nov 27, 2005
  4. Steven W. Orr

    Further adventures in array slicing.

    Steven W. Orr, May 4, 2007, in forum: Python
    Replies:
    3
    Views:
    279
    Alex Martelli
    May 5, 2007
  5. Aljosa Mohorovic
    Replies:
    0
    Views:
    279
    Aljosa Mohorovic
    Nov 27, 2009
Loading...

Share This Page