5.12.2. Slicing multidimensional arrays#

We just learned how to index multidimensional matrices using indexes separated by commas. Slicing works the same way: for a given dimension, all we need is to use a slice instead of an index.

Example 1: Read the marker’s y coordinate at samples 0 and 1

_images/dbb94d4f243561d91613e79e28e86594bc63b9390f7727e4a19d624d89c08a78.png
import numpy as np

position = np.array(
    [
        [0.497, 0.973, 0.010, 1.0],
        [0.528, 0.973, 0.017, 1.0],
        [0.589, 0.970, 0.025, 1.0],
    ]
)

position[0:2, 1]
array([0.973, 0.973])

Example 2: Read the marker’s y and z coordinates at samples 0 and 1

_images/642e60daf9d38f9b1e76ef3d17f7fc10a71a595326358fb33c1642462b56725a.png
position[0:2, 1:3]
array([[0.973, 0.01 ],
       [0.973, 0.017]])

Tip

A slice can be as simple as a column operator :. This is a slice with no bound, which literally means from the beginning up to the end. In other words, “all data on this axis”.

Example 3: Read the marker’s z coordinate at all samples

_images/5005982532577a8e8149ed903011a15368915daa11ece1d93f2667694403f505.png
position[:, 2]
array([0.01 , 0.017, 0.025])