Filtering multidimensional arrays

5.12.3. Filtering multidimensional arrays#

In section Filtering unidimensional arrays, we learned how to use lists of booleans and lists of integers to filter one-dimensional arrays. This also works on multidimensional arrays, although filtering on axes other than the first is somewhat unintuitive and can result in unexpected array shapes. Fortunately, in biomechanical data processing, filtering often occurs on the first axis, which typically corresponds to time.

Example: Read the marker coordinates at samples 0 and 2

_images/7eb4e5cd8f6beccbf876bc58a6b5bc2651ae81d14e1f3ab040d7d46e50374871.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.   ]])