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
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
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
position[:, 2]
array([0.01 , 0.017, 0.025])