2

I want to create an array in python with a fixed spacing between the values 2n and 2n+1, and another different spacing between the values 2n+1 and 2n+2.

I want to create an array like the following one, specifying just the initial point, the final point and the two different spacings that I want to use:

x = [0 0.25 0.75 1 1.5 1.75]

In this case those values would be 0 and 2 and (0.25,0.5) for the spacing.

Is it possible to do this using a predefined function?
I haven't found this functionality in the numpy package.

1
  • well if you want manual spacings that differ after each point, then you will have to write your own Commented Jul 22, 2015 at 14:01

3 Answers 3

4

Try this:

np.cumsum(np.tile([0.25, 0.5], 9))

If you want to prepend a zero, wrap it in np.insert(x, 0, 0).

Sign up to request clarification or add additional context in comments.

Comments

2

I think one problem is working out the number of elements, which may be ambiguous depending on what you want to happen at the end-points. This is an example of how you might handle it, though I will need to take another look later to see if it makes sense. I have taken the cumsum and tile combo from the answer by @Mauris.

import numpy as np

def spaced_array(start, end, spacings):
    step = sum(spacings)

    # how many steps in total can we take? (upper bound)
    repeats = int(np.ceil((end - start) / step))
    n = repeats * len(spacings)

    arr = np.full(n, start)
    arr[1:] += np.cumsum(np.tile(spacings, repeats))[:n-1]

    return arr

spacings = [0.25, 0.5]
print(spaced_array(0, 2, spacings))
print(spaced_array(0, 5, spacings))

Comments

2

Deciding the length of the output array for a generic case could be tricky, as also discussed in @YXD's solution. Assuming you want to end at the point that has the second spacing added, you could initialize a zeros array, put the starting value as the first element and then put the first and second spacing alternately and finally do cummulative summation to get the desired output. Here's an implementation to achieve something on those lines -

# Random inputs
start_pt = 0.25
final_pt = 2.85

spacing1 = 0.15
spacing2 = 0.50

# Estimate length of output array
N = 1+2*int(np.ceil((final_pt - start_pt)/(spacing1+spacing2)))

# Setup values array that holds the starting point as the first element
#  and then spacing1 and spacing2 alternately
vals = np.zeros(N)
vals[0] = start_pt
vals[1::2] = spacing1
vals[2::2] = spacing2

# Finally, do cumsum to get the desired output
out = vals.cumsum()

Output with the listed inputs -

In [134]: out
Out[134]: array([ 0.25,  0.4 ,  0.9 ,  1.05,  1.55,  1.7 ,  2.2 ,  2.35,  2.85])

1 Comment

I just find a minor drawback to this code. You always end your array with an element which is the previous one plus the second spacing you've chosen. But that is really easy to fix appending or removing an element at the end of the array.

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.