From 0502210e192018f7007dddabee9598e77c73a107 Mon Sep 17 00:00:00 2001 From: David Rotermund <54365609+davrot@users.noreply.github.com> Date: Fri, 22 Dec 2023 15:32:19 +0100 Subject: [PATCH] Update README.md Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com> --- numpy/reverse_an_axis/README.md | 97 +++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/numpy/reverse_an_axis/README.md b/numpy/reverse_an_axis/README.md index 0ece934..c5fec8b 100644 --- a/numpy/reverse_an_axis/README.md +++ b/numpy/reverse_an_axis/README.md @@ -125,3 +125,100 @@ Output [[0 1 2] [3 4 5]] ``` + +## [numpy.roll](https://numpy.org/doc/stable/reference/generated/numpy.roll.html) + +```python +numpy.roll(a, shift, axis=None) +``` + +> Roll array elements along a given axis. +> +> Elements that roll beyond the last position are re-introduced at the first. + +```python +import numpy as np + +a = np.arange(0, 6).reshape((2, 3)) +print(a) +print() +print(np.roll(a, shift=1)) +print() +print(np.roll(a, shift=2)) +print() +print(np.roll(a, shift=3)) +print() +print(np.roll(a, shift=4)) +``` + +Output: + +```python +[[0 1 2] + [3 4 5]] + +[[5 0 1] + [2 3 4]] + +[[4 5 0] + [1 2 3]] + +[[3 4 5] + [0 1 2]] + +[[2 3 4] + [5 0 1]] +``` + +```python +import numpy as np + +a = np.arange(0, 6).reshape((2, 3)) +print(a) +print() +print(np.roll(a, shift=1, axis=0)) +print() +print(np.roll(a, shift=2, axis=0)) +``` + +Output: + +```python +[[0 1 2] + [3 4 5]] + +[[3 4 5] + [0 1 2]] + +[[0 1 2] + [3 4 5]] +``` + +```python +import numpy as np + +a = np.arange(0, 6).reshape((2, 3)) +print(a) +print() +print(np.roll(a, shift=1, axis=1)) +print() +print(np.roll(a, shift=2, axis=1)) +print() +print(np.roll(a, shift=3, axis=1)) +``` + +Output: + +```python +[[0 1 2] + [3 4 5]] + +[[2 0 1] + [5 3 4]] + +[[1 2 0] + [4 5 3]] + +[[0 1 2] + [3 4 5]] +```