Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending values to a slice on an np array

I have an numpy array which I have initialized and looks like this:

pulse = np.ndarray(shape = (300, 10001, 3), dtype = float) 

I want to fill this array with some data I am reading from a file. An example of the way I want to fill it looks like this:

pulse[0][0][0] = 1
pulse[0][1:10001][0] = data

where data is an array of 10000 elements.

Could this be done using append or another function of numpy?

like image 312
AthinaPap Avatar asked Nov 30 '25 16:11

AthinaPap


2 Answers

The problem with you're current approach is that you're assigning to a copy of the data, and hence the original array remains unchanged. Instead assign to a view of the array (which is known as slice assignment), this way you'll be modifying in-place:

pulse[0, 1:10001, 0] = data
like image 101
yatu Avatar answered Dec 02 '25 04:12

yatu


This should work :

pulse[0,0,0]= 1
pulse[0,1:1001,0]= data
like image 28
Ferran Avatar answered Dec 02 '25 06:12

Ferran