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

Question:

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])
Asked By: thomas.mac

||

Answers:

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.        ])
Answered By: mozway
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.