pytutorial/numpy/slices_views
David Rotermund 0bd475f4d2
Update README.md
Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com>
2023-12-14 16:44:49 +01:00
..
README.md Update README.md 2023-12-14 16:44:49 +01:00

Slices and views

{:.no_toc}

* TOC {:toc}

The goal

Sometimes we want to use or see only a part of the matrix. This can be done via slices and views

Questions to David Rotermund

Reminder: 1-d slines

We assume N as the number of elements and 1d:

  • A valid index starts at 0 and runs until N-1
  • [start:stop:step] start = 1, stop=N, step=1 -> this results in the sequence 1,2,3,...,(N-1)
  • [start:stop:1] can be shortened to [start:stop]
  • [0:stop] can be shortened to [:stop]
  • [start:N] can be shortened to [start:]
  • B = A[:] or B = A[...] gives you a view of A. B has the same shape and size of A.
import numpy as np

a = np.arange(0, 10)
print(a[1:10:1])  # -> [1 2 3 4 5 6 7 8 9]
print(a[3:7:2])  # -> [3 5]
print(a[3:6])  # -> [3 4 5]
print(a[:6])  # -> [0 1 2 3 4 5]
print(a[5:])  # -> [5 6 7 8 9]
print(a[:])  # -> [0 1 2 3 4 5 6 7 8 9]
print(a[...])  # -> [0 1 2 3 4 5 6 7 8 9]
print(a[:9999])  # -> [0 1 2 3 4 5 6 7 8 9]
print(a[9999:])  # ->[]
  • Negative values for start and stop are understood as N-|start| and N-|stop|
  • N-1 is the last valid index.
  • Thus A[-1] gives us the last element of A.

Extracting a value based on a negative index:

import numpy as np

a = np.arange(0, 10)
print(a[-1])  # -> 9
print(a[-2])  # -> 8
print(a[-9])  # -> 1
print(a[-10])  # -> 0
print(a[-11])  # IndexError: index -11 is out of bounds for axis 0 with size 10

Extracting a slice based on a negative stop point:

import numpy as np

a = np.arange(0, 10)
print(a)  # -> [0 1 2 3 4 5 6 7 8 9]
print(a[:-1])  # -> [0 1 2 3 4 5 6 7 8]
print(a[:-5])  # -> [0 1 2 3 4]
print(a[:-8])  # -> [0 1]
print(a[:-11])  # -> []
print(a[:-12])  # -> []
print(a[:-999])  # -> []

Extracting a slice based on a negative start point:

import numpy as np

a = np.arange(0, 10)
print(a)  # -> [0 1 2 3 4 5 6 7 8 9]
print(a[-3:-1])  # -> [7 8]
print(a[-1:-8])  # -> []
print(a[-9999:])  # -> [0 1 2 3 4 5 6 7 8 9]

Negative step sizes:

import numpy as np

a = np.arange(0, 10)
print(a)  # -> [0 1 2 3 4 5 6 7 8 9]
print(a[::-1])  # -> [9 8 7 6 5 4 3 2 1 0]
print(a[4:-2:-1]) # -> []
print(a[-1:5:-1]) # -> [9 8 7 6]