sam
sam

Reputation: 19164

creating timesliced array in numpy

I want to create a numpy array.

T = 200

I want to create an array from 0 to 199, in which each value will be divided by 200.

l = [0, 1/200, 2/200, ...]

Numpy have any such method for calculation?

Upvotes: 1

Views: 61

Answers (4)

alko
alko

Reputation: 48297

Alternatively one can use linspace:

>>> np.linspace(0, 1., 200, endpoint=False)
array([ 0.   ,  0.005,  0.01 ,  0.015,  0.02 ,  0.025,  0.03 ,  0.035,
        0.04 ,  0.045,  0.05 ,  0.055,  0.06 ,  0.065,  0.07 ,  0.075,
          ...
        0.92 ,  0.925,  0.93 ,  0.935,  0.94 ,  0.945,  0.95 ,  0.955,
        0.96 ,  0.965,  0.97 ,  0.975,  0.98 ,  0.985,  0.99 ,  0.995])

Upvotes: 3

Ian
Ian

Reputation: 1503

import numpy as np
T = 200
np.linspace(0.0, 1.0 - 1.0 / float(T), T)

Personally I prefer linspace for creating evenly spaced arrays in general. It is more complex in this case as the endpoint depends on the number of points T.

Upvotes: 1

synthomat
synthomat

Reputation: 774

T = 200.0
l = [x / float(T) for x in range(200)]

Upvotes: 1

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250881

Use np.arange:

>>> import numpy as np  
>>> np.arange(200, dtype=np.float)/200
array([ 0.   ,  0.005,  0.01 ,  0.015,  0.02 ,  0.025,  0.03 ,  0.035,
        0.04 ,  0.045,  0.05 ,  0.055,  0.06 ,  0.065,  0.07 ,  0.075,
        0.08 ,  0.085,  0.09 ,  0.095,  0.1  ,  0.105,  0.11 ,  0.115,
        ...
        0.88 ,  0.885,  0.89 ,  0.895,  0.9  ,  0.905,  0.91 ,  0.915,
        0.92 ,  0.925,  0.93 ,  0.935,  0.94 ,  0.945,  0.95 ,  0.955,
        0.96 ,  0.965,  0.97 ,  0.975,  0.98 ,  0.985,  0.99 ,  0.995])

Upvotes: 2

Related Questions