Filtering multidimensional arrays

5.12.3. Filtering multidimensional arrays#

In section Filtering unidimensional arrays, we learned how to use lists of boolean and lists of integers to filter unidimensional arrays. This also works on multidimensional arrays, although filtering on axes other than the first one is somewhat unintuitive and may lead to unexpected array shapes. Luckily, in biomechanical data processing, filtering happens mainly on the first axis, which usually corresponds to time.

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

_images/480f28534ddb3f8197088b310a1fbb755c25bf9c69bc127c5996d81802456871.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],
    ]
)

mask = [0, 2]
position[mask]
array([[0.497, 0.973, 0.01 , 1.   ],
       [0.589, 0.97 , 0.025, 1.   ]])

or directly:

position[[0, 2]]
array([[0.497, 0.973, 0.01 , 1.   ],
       [0.589, 0.97 , 0.025, 1.   ]])