Reputation: 55
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?
Upvotes: 3
Views: 1381
Reputation: 66
pulse[0][0][0] = 1
pulse[0][1:10001][0] = data
this will work. the dimension of data must be exactly the size of the slice. Also instead of assigning an array, if you assign a constant, all the elements in the slice will be assigned that value.
Upvotes: 1
Reputation: 88295
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
Upvotes: 1