Skip to content Skip to sidebar Skip to footer

Python/Numpy: Vectorizing Repeated Row Insertion In A 2D Array

Is it possible to vectorize the insertion of rows? I have a large 2D numpy array arr (below) and a list of indices. For each index of arr in indices I would like to insert the row

Solution 1:

You could use np.repeat for this:

indices = [2, 4, 5, 9, 11, 12, 16, 18, 19]
rpt = np.ones(len(arr), dtype=int)
rpt[indices] = 5

np.repeat(arr, rpt, axis=0)

Post a Comment for "Python/Numpy: Vectorizing Repeated Row Insertion In A 2D Array"