5.11.1. Exercise: Indexing/slicing/filtering unidimensional arrays 1#

We measured the step length of a person during 10 steps. Here are these measurements:

import numpy as np

step_length = np.array(
    [0.707, 0.730, 0.752, 0.707, 0.691, 0.726, 0.722, 0.726, 0.710, 0.661]
)  # in meters

For each of these questions, write a single line of code that prints the requested step lengths:

  1. From the third step up to the end;

  2. The two last steps;

  3. All steps, but without the two firsts and the two lasts;

  4. Every other step starting from the third;

  5. Steps 2, 5, 6, 7, with step 0 being the first.

  6. The first and the last.

Hide code cell content
# 1. From the third step up to the end;
print(step_length[2:])

# 2. The two last steps;
print(step_length[-2:])

# 3. All steps, but without the two firsts and the two lasts;
print(step_length[2:-2])

# 4. Every other step starting from the third;
print(step_length[2::2])

# 5. Steps 2, 5, 6, 7, with step 0 being the first.
print(step_length[[2, 5, 6, 7]])
# Note the double bracket: this is equivalent to write:
# >> mask = [2, 4, 6, 7]
# >> step_length[mask]

# 6. The first and the last.
print(step_length[[0, -1]])
[0.752 0.707 0.691 0.726 0.722 0.726 0.71  0.661]
[0.71  0.661]
[0.752 0.707 0.691 0.726 0.722 0.726]
[0.752 0.691 0.722 0.71 ]
[0.752 0.726 0.722 0.726]
[0.707 0.661]