1

I would like to be able to shift a 3D numpy array in either direction along the height axis. If a positive shift of a say 3 is given, all values in the array shift up and 3 new slices of zeros appear at the bottom of the array. The opposite would occur if a negative shift is given, all slices shift down along the height axis and new slices of zeros appear at the top.

I have attempted reshaping the array to 1D and shifting left and right, and then returning to 3D. However, this shifts the array along the wrong direction. I tried implementing numpy.rollaxis before reshaping, thus shifting along the correct axis. But the system I'm working on does not recognize all numpy functions and will not perform rollaxis.

Thanks!

3
  • Can you post your attempt so far and let us know what error you are encountering with it? Commented Apr 9, 2013 at 8:14
  • See stackoverflow.com/questions/2777907/… Commented Apr 9, 2013 at 9:49
  • Also what do you mean by "But the system I'm working on does not recognize all numpy functions"? Commented Apr 9, 2013 at 10:12

1 Answer 1

4

Your question is a little ill defined. First, the 'height' axis is not a very clear definition of which one you are after in a 3D array. It is much better to locate them by position in the shape tuple. I am going to take this as meaning 'along the first axis', but it should be obvious how to do it for a different one.

Second, while it is clear that you want zeros to fill in the shifted data, you don't specify what you want to do with data on the other side: should it disappear beyond the array's boundary and be lost? Or should the array be extended to keep it all?

For the former, numpy has the roll function, which does similar to what you want, but instead of filling in with zeros, it copies the data from the other side of the array. You can simply replace this with zeros afterwards:

>>> a = np.arange(60).reshape(3, 4, 5)
>>> a
array([[[ 0,  1,  2,  3,  4],
        [ 5,  6,  7,  8,  9],
        [10, 11, 12, 13, 14],
        [15, 16, 17, 18, 19]],

       [[20, 21, 22, 23, 24],
        [25, 26, 27, 28, 29],
        [30, 31, 32, 33, 34],
        [35, 36, 37, 38, 39]],

       [[40, 41, 42, 43, 44],
        [45, 46, 47, 48, 49],
        [50, 51, 52, 53, 54],
        [55, 56, 57, 58, 59]]])
>>> b = np.roll(a, 2, axis=0)
>>> b[:2,:, :] = 0
>>> b
array([[[ 0,  0,  0,  0,  0],
        [ 0,  0,  0,  0,  0],
        [ 0,  0,  0,  0,  0],
        [ 0,  0,  0,  0,  0]],

       [[ 0,  0,  0,  0,  0],
        [ 0,  0,  0,  0,  0],
        [ 0,  0,  0,  0,  0],
        [ 0,  0,  0,  0,  0]],

       [[ 0,  1,  2,  3,  4],
        [ 5,  6,  7,  8,  9],
        [10, 11, 12, 13, 14],
        [15, 16, 17, 18, 19]]])

Had the shift been negative, instead of b[:shift, :, :] = 0 you would go with b[-shift:, :, :] = 0.

If you don't want to lose data, then you are basically just adding slices of zeros at the top or bottom of the array. For the first three dimensions, numpy has vstack, hstack, and dstack, and vstack should be the one you are after:

>>> a = np.arange(60).reshape(3, 4, 5)
>>> b = np.vstack((np.zeros((2,)+a.shape[1:], dtype=a.dtype), a))
>>> b
array([[[ 0,  0,  0,  0,  0],
        [ 0,  0,  0,  0,  0],
        [ 0,  0,  0,  0,  0],
        [ 0,  0,  0,  0,  0]],

       [[ 0,  0,  0,  0,  0],
        [ 0,  0,  0,  0,  0],
        [ 0,  0,  0,  0,  0],
        [ 0,  0,  0,  0,  0]],

       [[ 0,  1,  2,  3,  4],
        [ 5,  6,  7,  8,  9],
        [10, 11, 12, 13, 14],
        [15, 16, 17, 18, 19]],

       [[20, 21, 22, 23, 24],
        [25, 26, 27, 28, 29],
        [30, 31, 32, 33, 34],
        [35, 36, 37, 38, 39]],

       [[40, 41, 42, 43, 44],
        [45, 46, 47, 48, 49],
        [50, 51, 52, 53, 54],
        [55, 56, 57, 58, 59]]])

If you wanted the zeros appended at the bottom, simply change the order of the parameters in the call to the stacking function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.