3

I have the following data

a= [1 1.1 1.2 1.3 1.4 1.5]

What I want to do is for each value of this data produce a series of points in increments with a spacing of 10%. Creating a new array:

b=  [[0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0], [0 0.11 ... 1.1],.....]

The next thing that I want to do is is take each number from List 1 and determine the number of increments (20% spacing) from another value e.g. 2, to get another array:

c=[[1 1.2 1.4. 1.6 1.8 2.0], [1.1 ..... 2.0],......]

I then want to combine these arrays:

  d =[[0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1 1.2 1.4. 1.6 1.8 2.0], [0 0.11 ... 2.0],.....]

List 1 is determined from an equation, but I want to do further calculations up to a certain point in this case a value of 2.

Would something arange work or some other way generating a sequence of numbers? Is this even possible?

1
  • Be aware that x.1, x.2, x.3, x.4, x.6, x.7, x.8, x.9 can't be represented exactly as floats. Commented Mar 5, 2013 at 1:07

1 Answer 1

2

Mixing list comprehensions and np.linspace it is pretty straightforward:

>>> a = [1, 1.1, 1.2, 1.3, 1.4, 1.5]

>>> b = [np.linspace(0, j, 11) for j in a]
>>> b
[array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9,  1. ]),
 array([ 0.  ,  0.11,  0.22,  0.33,  0.44,  0.55,  0.66,  0.77,  0.88,  0.99,
         1.1 ]),
 ...
 array([ 0.  ,  0.15,  0.3 ,  0.45,  0.6 ,  0.75,  0.9 ,  1.05,  1.2 ,  1.35,
         1.5 ])]

>>> c = [np.linspace(j, 2, 6) for j in a]
>>> c
[array([ 1. ,  1.2,  1.4,  1.6,  1.8,  2. ]),
 array([ 1.1 ,  1.28,  1.46,  1.64,  1.82,  2.  ]),
 ...
 array([ 1.5,  1.6,  1.7,  1.8,  1.9,  2. ])]

To concatenate them you must either remove the first element of every array in c or the last of every array in b. If you only need the concatenation, I would suggest keeping c as above, and doing:

>>> b = [np.linspace(0, j, 10, endpoint=False) for j in a]

>>> d = map(np.concatenate, zip(b, c))
>>> d
[array([ 0. ,  0.1,  0.2,  0.3,  0.4,  0.5,  0.6,  0.7,  0.8,  0.9,  1. ,
         1.2,  1.4,  1.6,  1.8,  2. ]),
 array([ 0.  ,  0.11,  0.22,  0.33,  0.44,  0.55,  0.66,  0.77,  0.88,
         0.99,  1.1 ,  1.28,  1.46,  1.64,  1.82,  2.  ]),
 ...
 array([ 0.  ,  0.15,  0.3 ,  0.45,  0.6 ,  0.75,  0.9 ,  1.05,  1.2 ,
         1.35,  1.5 ,  1.6 ,  1.7 ,  1.8 ,  1.9 ,  2.  ])]

If you want lists instead of numpy arrays you can always do a final

>>> d = map(list, d)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.