I would like to insert some rows and columns into a numpy array.
If I have a square array of length n_a, for example: n_a = 3
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
and I would like to get a new array with size n_b that contains an array a and zeros (or any other 1-d array of length n_b) for specific rows and columns with indexes, for example.
index = [1, 3]
so n_b = n_a + len (index). Then a new array:
b = np.array([[1, 0, 2, 0, 3], [0, 0, 0, 0, 0], [4, 0, 5, 0, 6], [0, 0, 0, 0, 0], [7, 0, 8, 0, 9]])
So my question is how to do this efficiently, based on the assumption that the larger n_a arrays are much larger than len (index).
EDIT
Results for:
import numpy as np import random n_a = 5000 n_index = 100 a=np.random.rand(n_a, n_a) index = random.sample(range(n_a), n_index)
Warren Walkesser Solution: 0.208 s
Wim solution: 0.980 s
Ashwini Chaudhary Solution: 0.955 s
Thanks everyone!