I have a 2D numpy array of zeros, a list of numpy arrays (which can be of different lengths) and a list of indices. I want to add the contents of each of the arrays in the list from the start of the corresponding row indice in the 2D array.
Of course, I can just loop over the arrays. However, I need to perform this operations for many different samples. Therefore, I was wondering whether anybody is aware of a more efficient way to do this.
In [1]: A = np.zeros((5, 5))
...: arrays = [np.random.randint(1, 10, size=(1,5)) for i in range(3)]
...: indices = [1, 3, 4]
...: print(arrays)
Out[1]:
[array([3, 1, 3, 6]), array([4, 9]), array([5, 9, 6])]
Expected output:
array([[0., 0., 0., 0., 0.],
[3., 1., 3., 6., 0.],
[0., 0., 0., 0., 0.],
[4., 9., 0., 0., 0.],
[5., 9., 6., 0., 0.]]
Any help would be much appreciated!
zipand iterate?hstack(array)and[len(i) for i in arrays]to map the arrays ontoAin one step. Yourindicescomplicates things, though it might be easier to useindicesto remapAafter padding.