2

My aim is to rotate a 1d numpy array by left.For example desired output for numpy array [1,2,3,4] should be [2,3,4,1].

Here is my approach:

import numpy as np

x = np.array([1,2,3,4])
x1 = x[1:]
x2 = x[:1]
print(np.concatenate(x1,x2))

I am facing an error while concatenating. Why is that?

I tried this approach also:

lst = x[1:] + x[:1]
print(np.array(lst))

Although I am getting the desired output I am also getting a error

DeprecationWarning: elementwise comparison failed; this will raise an error in the future.

2
  • 1
    extra pair of brackets? np.concatenate([x1,x2]) Commented Jul 2, 2020 at 16:26
  • @PaulPanzer Oh yeah silly me.That was the problem Commented Jul 2, 2020 at 16:31

1 Answer 1

1

I already saw this kind of problem to build AES in python. But I didn't used numpy, only list: the step is called ShiftRows

Anyway, you can use: np.roll(your_array, int_shift) For example:

>>> x = np.array([1,2,3,4])
>>> shift_x = np.roll(x, 3)
>>> shift_x
>>> array([2, 3, 4, 1])
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.