Numeric, vectorization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RonnyM

    #1

    Numeric, vectorization

    Hello.

    I want to vectorize this operation, which below is implemented as a
    for-loop:

    def smoothing_loop( y ): #y is an array with noisy values
    ybar = []
    ybar.append( y[0] )
    #Smoothing with a loop
    length = size( y )
    for i in range( 1, length -1 ):
    ybar.append( .5 * ( y[ i-1 ] + y[ i + 1 ] ) )

    e.g. y = [ 1, 2, 3, 4, 5, 6 ,7 ,8, 9 ]

    ybar = [ 1, (1 + 3)*.5,(2 + 4)*.5,(3 + 5)*.5,..., (n-1 + n+1)*.5 ], n =
    1,...len(y) -1

    How do I make a vectorized version of this, I will prefer not to
    utilize Map or similar functions, but numeric instead.


    Regards,

    Ronny Mandal

  • David Isaac

    #2
    Re: Numeric, vectorization

    "RonnyM" <ronnyma@math.u io.no> wrote in message
    news:1146500254 .216411.166700@ u72g2000cwu.goo glegroups.com.. .[color=blue]
    > e.g. y = [ 1, 2, 3, 4, 5, 6 ,7 ,8, 9 ][/color]
    [color=blue]
    > ybar = [ 1, (1 + 3)*.5,(2 + 4)*.5,(3 + 5)*.5,..., (n-1 + n+1)*.5 ], n =
    > 1,...len(y) -1
    > How do I make a vectorized version of this, I will prefer not to
    > utilize Map or similar functions, but numeric instead.[/color]


    You treat the first element asymmetrically, so that does not vectorize.
    The rest does:[color=blue][color=green][color=darkred]
    >>> import numpy as N
    >>> y=N.arange(1,10 )
    >>> slice1 = slice(0,-2,1)
    >>> slice2 = slice(2,None,1)
    >>> ybar = 0.5*(y[slice1]+y[slice2])
    >>> ybar[/color][/color][/color]
    array([ 2., 3., 4., 5., 6., 7., 8.])

    hth,
    Alan Isaac


    Comment

    Working...