1

I have a numpy array:

arr=np.array([[1., 2., 0.],
               [2., 4., 1.],
               [1., 3., 2.],
               [-1., -2., 4.],
               [-1., -2., 5.],
               [1., 2., 6.]])

I want to flip the second half of this array upward. I mean I want to have:

flipped_arr=np.array([[-1., -2., 4.],
                      [-1., -2., 5.],
                      [1., 2., 6.],
                      [1., 2., 0.],
                      [2., 4., 1.],
                      [1., 3., 2.]])

When I try this code:

fliped_arr=np.flip(arr, 0)

It gives me:

fliped_arr= array([[1., 2., 6.],
                   [-1., -2., 5.],
                   [-1., -2., 4.],
                   [1., 3., 2.],
                   [2., 4., 1.],
                   [1., 2., 0.]])

In advance, I do appreciate any help.

2
  • 1
    What is the expected behaviour when the number of rows in the array is odd? Commented Dec 3, 2020 at 14:43
  • @Kalpit, Thanks for your hint. Then I want to say from that line until the end, bring rows with the same order up. Then that nth line will be the first one. Commented Dec 3, 2020 at 14:44

3 Answers 3

3

You can simply concatenate rows below the nth row (included) with np.r_ for instance, with row index n of your choice, at the top and the other ones at the bottom:

import numpy as np
n = 3

arr_flip_n = np.r_[arr[n:],arr[:n]]

>>> array([[-1., -2.,  4.],
           [-1., -2.,  5.],
           [ 1.,  2.,  6.],
           [ 1.,  2.,  0.],
           [ 2.,  4.,  1.],
           [ 1.,  3.,  2.]])
Sign up to request clarification or add additional context in comments.

Comments

2

you can do this by slicing the array using the midpoint:

ans = np.vstack((arr[int(arr.shape[0]/2):], arr[:int(arr.shape[0]/2)]))

to break this down a little:

find the midpoint of arr, by finding its shape, the first index of which is the number of rows, dividing by two and converting to an integer:

midpoint = int(arr.shape[0]/2)

the two halves of the array can then be sliced like so:

a = arr[:midpoint]
b = arr[midpoint:]

then stack them back together using np.vstack:

ans = np.vstack((a, b))

(note vstack takes a single argument, which is a tuple containing a and b: (a, b))

2 Comments

Hi @Finley, how is this different than my answer?
Doesn't seem to be. Your's wasn't there at the time of posting. (appeared upon the refresh that came with posting the asnwer)
1

You can do this with array slicing and vstack -

arr=np.array([[1., 2., 0.],
               [2., 4., 1.],
               [1., 3., 2.],
               [-1., -2., 4.],
               [-1., -2., 5.],
               [1., 2., 6.]])

mid = arr.shape[0]//2  
np.vstack([arr[mid:],arr[:mid]])
array([[-1., -2.,  4.],
       [-1., -2.,  5.],
       [ 1.,  2.,  6.],
       [ 1.,  2.,  0.],
       [ 2.,  4.,  1.],
       [ 1.,  3.,  2.]])

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.