I am looking for the fastest way (preferably with numpy) to delete a list of indices in each row of a 2D array. As an example:
matrix = [[1,2,3,4,5],
[4,5,6,7,8],
[7,8,9,10,11]]
indices_to_delete = [2,3]
And now the goal is to delete these indices form each row, to get:
result = [[1,2,5],
[4,5,8],
[7,8,11]]
My current approach would be to do this separately for each row using:
result = []
for row in array:
result.append(np.delete(row, indices_to_delete))
Is there a faster way of doing this?
CodePudding user response:
You can use .delete
along different axis:
>>> np.delete(matrix, indices_to_delete, axis=1)
array([[ 1, 2, 5],
[ 4, 5, 8],
[ 7, 8, 11]])