1

I have a NumPy array 'data' as follows:

data = np.array([
[0.0, 30.0, 60.0, 90.0, 120.0, 150.0, -180.0, -150.0, -120.0, -90.0, -60.0, -30.0],
[0.0, 30.0, 60.0, 90.0, 120.0, 150.0, -180.0, -150.0, -120.0, -90.0, -60.0, -30.0],
[0.0, 30.0, 60.0, 90.0, 120.0, 150.0, -180.0, -150.0, -120.0, -90.0, -60.0, -30.0]])

I want to produce the array 'result' from the given array 'data'. Actually, in the required array, the zero column has to be put on the middle, then the values are increasing towards right direction, while the values are decreasing in the left direction as shown below:

result = np.array([
[-180.0, -150.0, -120.0, -90.0, -60.0, -30.0, 0.0, 30.0, 60.0, 90.0, 120.0, 150.0, 180.0],
[-180.0, -150.0, -120.0, -90.0, -60.0, -30.0, 0.0, 30.0, 60.0, 90.0, 120.0, 150.0, 180.0],
[-180.0, -150.0, -120.0, -90.0, -60.0, -30.0, 0.0, 30.0, 60.0, 90.0, 120.0, 150.0, 180.0]])

The resulted array should be based on index manipulation of the given array. What is the best way of doing it in NumPy?

I tried np.rot90, np.flipud, np.fliprl functions with no success.

However, I could not think how to start.

2 Answers 2

1

Take a look at np.roll:

np.roll(data, shift=data.shape[1]//2, axis=1)

Here the shift is saying how many elements to roll the data by (Positive values to the right, negative to the left). Given the specification, we want to roll it to the right by half the length of the array along the second dimension (axis=1). The // is integer division, and data.shape[1] gets the size of the dimension along the second dimension (based on zero indexing).

I think you're missing the +180.0 value that you have in result in data though (i.e your result has 13 columns, but your data has only 12).

In [9]: data = np.array([
[0.0, 30.0, 60.0, 90.0, 120.0, 150.0, -180.0, -150.0, -120.0, -90.0, -60.0, -30.0],
[0.0, 30.0, 60.0, 90.0, 120.0, 150.0, -180.0, -150.0, -120.0, -90.0, -60.0, -30.0],
[0.0, 30.0, 60.0, 90.0, 120.0, 150.0, -180.0, -150.0, -120.0, -90.0, -60.0, -30.0]])

In [10]: result = np.roll(data, data.shape[1]//2, axis=1)

In [11]: result
Out[11]:
array([[-180., -150., -120.,  -90.,  -60.,  -30.,    0.,   30.,   60.,
          90.,  120.,  150.],
       [-180., -150., -120.,  -90.,  -60.,  -30.,    0.,   30.,   60.,
          90.,  120.,  150.],
       [-180., -150., -120.,  -90.,  -60.,  -30.,    0.,   30.,   60.,
          90.,  120.,  150.]])

This isn't really sorting based on index, but given the other methods you tried, I'm guessing this is the type manipulation that you might want.

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

2 Comments

great! accepted. BTW could you please explain me little more about data.shape[1]//2 and axis=1
Sure, added a few notes on this. Also take a look at the numpy documentation for more details.
0

So, I am guessing that you're trying to sort this array from least to highest value? It isn't entirely clear from your question. Try this?

result = []
for row in data:
    result.append(np.sort(row))

You can use the facilities in numpy to easily convert 'result' into a numpy array again. There is more information about np.sort available here

2 Comments

Nope, actually I was looking for the same what @JoshAdel has answered
My bad. Glad you found your answer.

Your Answer

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