I have a data matrix a and I have list of indices stored in array idx. I would like to get 10-length data starting at each of the indices defined by idx . Right now I use a for loop to achieve this. But it is extremely slow as I have to do this data fetch about 1000 times in an iteration. Below is a minimum working example.
import numpy as np
a = np.random.random(1000)
idx = np.array([1, 5, 89, 54])
# I want "data" array to have np.array([a[1:11], a[5:15], a[89:99], a[54:64]])
# I use for loop below but it is slow
data = []
for id in idx:
data.append(a[id:id+10])
data = np.array(data)
Is there anyway to speed up this process? Thanks.
EDIT: My question is different from the question asked here. In the question, the size of the chunks is random in contrast to fixed chunk size in my question. Other differences exist. I do not have to use up the entire array a and an element can occur in more than one chunk. My question does not necessarily "split" the array.
ais ones for the purpose of the question, is that right?np.split(a, idx)you will split the array on indices1,5leaving you with[array of size 1, array of size 4, ...which is not the desired result.