Repeating array with transformation

  • A+
Category:Languages

How can I repeat the following example sequence:

l = np.array([3,4,5,6,7]) 

Up to n times, doubling the values on each repetition. So for n=3:

[3, 4, 5, 6, 7, 6,  8, 10, 12, 14, 12, 16, 20, 24, 28] 

Is there a simple way avoiding loops with numpy perhaps?

 


numpy.outer + numpy.ndarray.ravel:

>>> a = np.array([3,4,5,6,7])                                                                                           >>> n = 3                                                                                                               >>> factors = 2**np.arange(n)                                                                                           >>> np.outer(factors, a).ravel()                                                                                        array([ 3,  4,  5,  6,  7,  6,  8, 10, 12, 14, 12, 16, 20, 24, 28]) 

Details:

>>> factors                                                                                                             array([1, 2, 4]) >>> np.outer(factors, a)                                                                                                array([[ 3,  4,  5,  6,  7], # 1*a        [ 6,  8, 10, 12, 14], # 2*a        [12, 16, 20, 24, 28]]) # 4*a 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: