I am trying to append a number to a particular row of a 2D numpy array. I did the following but it doesn't work right:
onspiketimes=np.array([[] for i in range(9)])
neurs = [3, 4, 8, 2]
onspiketimes[neurs]=2
>>> onspiketimes
array([], shape=(9, 0), dtype=float64)
As you can see that nothing got appended,onspiketimes still remains empty.
However I could do this with list of lists as below:
>>> onspiketimes= [[] for i in range(9)]
>>> for items in neurs:
onspiketimes[items].append(2)
>>> for items in neurs:
onspiketimes[items].append(3)
>>> onspiketimes
[[], [], [2, 3], [2, 3], [2, 3], [], [], [], [2, 3]]
This is a contrived example, I can't really compare numpy and lists here but in my real applications for loops will really effect the speedup. I was hoping to solve this using array slicing in numpy.
maphere to append to a specific list if i just stick with list of lists?onspiketimesto something larger than you would anticipate needing. Then you can fill it in as you go. Ideally, you would add all the data at once rather than in increments if you want to speed up the process.