how to get a wrap aound slice of numpy array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • danfan1981@yahoo.com

    how to get a wrap aound slice of numpy array

    Hi, I am learning Numpy and using it for a course project.
    Suppose x = [0, 1, 2, 3, 4], I know that x[0:2] would give [0,1], and
    x[-1] give [4], is there a way that I can get [4,0,1]. I try x[-1:2],
    but it returns an empty array.

    I am doing convolution (e.g. convolve [0, 1, 2, 3, 4] with [1,-2,1])
    with wrap-around boundary condition, so sometimes the indices would
    wrap around the boundaries, and sometimes not. I would like to find a
    generic expression for both cases.

  • Robert Kern

    #2
    Re: how to get a wrap aound slice of numpy array

    danfan1981@yaho o.com wrote:
    Hi, I am learning Numpy and using it for a course project.
    Welcome! You might want to ask more numpy questions on the numpy mailing list.
    Answers to numpy questions here tend to be hit-or-miss.


    Suppose x = [0, 1, 2, 3, 4], I know that x[0:2] would give [0,1], and
    x[-1] give [4], is there a way that I can get [4,0,1]. I try x[-1:2],
    but it returns an empty array.
    x[[-1, 0, 1]]

    --
    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

    Comment

    • danfan1981@yahoo.com

      #3
      Re: how to get a wrap aound slice of numpy array

      Thanks. I didn't know numpy can do vector indexing as in Matlab.

      Comment

      Working...