AthinaPap
AthinaPap

Reputation: 55

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?

Upvotes: 3

Views: 1381

Answers (3)

EBIN JOSEPH
EBIN JOSEPH

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

Ferran
Ferran

Reputation: 840

This should work :

pulse[0,0,0]= 1
pulse[0,1:1001,0]= data

Upvotes: 1

yatu
yatu

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

Related Questions