Slicing multidimensional arrays

5.12.2. Slicing multidimensional arrays#

We just learned how to index multidimensional matrices using indices 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/9d664c141ae0acd8cb1dd02ac02e64686e18b0c0f56594bc0de833ca3f8404b7.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/27d22492c1367cec0b55be20cb59e4ed165146c072bc377a7f8f492ef498765b.png
position[0:2, 1:3]
array([[0.973, 0.01 ],
       [0.973, 0.017]])

Tip

A slice can be as simple as a colon operator :. This is a slice with no bounds, 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/4ce7b04f7166f9fdbe5b5c0ef35d1d3b8591fee07e688d3c84ce55e940f794d2.png
position[:, 2]
array([0.01 , 0.017, 0.025])