I am looking for a way to select multiple rows from a numpy array multiple times given an array of indexes.
Given M
and indexes
, I would like to get N
avoiding for loop, since it is slow for big dimensions.
import numpy as np
M = np.array([[1, 0, 1, 1, 0],
[1, 1, 1, 1, 0],
[0, 0, 0, 1, 1],
[1, 0, 0, 1, 1]])
indexes = np.array([[True, False, False, True],
[False, True, True, True],
[False, False, True, False],
[True, True, False, True]])
N = [M[index] for index in indexes]
Out:
[array([[1, 0, 1, 1, 0],
[1, 0, 0, 1, 1]]),
array([[1, 1, 1, 1, 0],
[0, 0, 0, 1, 1],
[1, 0, 0, 1, 1]]),
array([[0, 0, 0, 1, 1]]),
array([[1, 0, 1, 1, 0],
[1, 1, 1, 1, 0],
[1, 0, 0, 1, 1]])]