Skip to content Skip to sidebar Skip to footer

Python: Generate Unevenly Spaced Array

I would like to generate an array bounded by a lower and upper value with n elements similar to: def my_lin(lb, ub, steps): dx = (ub-lb) / (steps-1) return [lb + i*dx for i

Solution 1:

Find a function f which has your desired spacing within [0,1] (note that f(0)==0 and f(1)==1 should hold) and then apply f(i*dx/(ub-lb))*(ub-lb).

I personally like power functions for this but there is many more. For example, try this:

defmy_lin(lb, ub, steps, spacing=1.1):
    span = (ub-lb)
    dx = 1.0 / (steps-1)
    return [lb + (i*dx)**spacing*span for i inrange(steps)]

lin = my_lin(0, 10, 11)
print lin
# visualize the spacingprint''.join(' '*int(2*a)+'*'for a in lin)

Prints:

[0.0, 0.7943282347242814, 1.702679845041569, 2.6597044516956405, 3.6497741462219233, 4.665164957684037, 5.701201299034059, 6.754727665838724, 7.823462148343428, 8.905673323855929, 10.0]
**   **       **           **               **                    *

Now with the parameter spacing it works like this:

  • The closer you get to zero, the more your values gather around ub
  • The farther you go away from 1 into the other direction, the more they gather around lb
  • If spacing is exactly 1, they are equally spaced

Post a Comment for "Python: Generate Unevenly Spaced Array"