Home > Back-end >  Weight python array so a[0] is x times the size of a[-1]
Weight python array so a[0] is x times the size of a[-1]

Time:09-03

Hi all looking to weight an array so that the first value , a[0] , is x times the size of the last value, a[-1] . Additionally, i'd like this to be linearly decayed, ie equal parts between each value in the array.

mult = 3
a = [1, 1, 1, 1]

# These conditions should hold true:
# a[0] * 3 = a[-1]
# (a[1] - a[0]) == (a[2] - a[1])

CodePudding user response:

Use numpy.linspace:

out = np.linspace(mult*a[-1], a[-1], len(a))
# or
# out = np.linspace(mult, 1, len(a))

Output:

array([3.        , 2.33333333, 1.66666667, 1.        ])
  • Related