MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: moving average function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg126353] Re: moving average function
  • From: Ray Koopman <koopman at sfu.ca>
  • Date: Thu, 3 May 2012 04:38:03 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <jnlj94$n3k$1@smc.vnet.net> <jnoa47$547$1@smc.vnet.net> <jnqvr0$j73$1@smc.vnet.net>

On May 2, 2:47 am, rtmphone09 at gmail.com wrote:
> This is intriguing, but the length of the returned list doesn't match the length of the original list.
>
> Length /@ {testList, bma[testList, 5]}
>
> {11, 15}

I think this gives the output you want, but
it's just a cleaned-up version of your code.

mab[data_List, n_Integer?OddQ] := Join[
    Mean@Take[data, #]& /@ Range[1,n-2, 2],
    MovingAverage[data, n],
    Mean@Take[data,-#]& /@ Range[n-2,1,-2]]

mab[testList, 5]

a
(a + b + c)/3
(a + b + c + d + e)/5
(b + c + d + e + f)/5
(c + d + e + f + g)/5
(d + e + f + g + h)/5
(e + f + g + h + i)/5
(f + g + h + i + j)/5
(g + h + i + j + k)/5
(i + j + k)/3
k

An interesting error occurred in an earlier version.
I ran into this once before. See "Insulating data from code"
http://forums.wolfram.com/mathgroup/archive/2006/May/msg00368.html

bad[data_List, n_Integer?OddQ] := Join[
    Table[Mean@Take[data, k], {k,1,n-2, 2}],
    MovingAverage[data, n],
    Table[Mean@Take[data,-k], {k,n-2,1,-2}]]

bad[testList, 5]

a
(a + b + c)/3
(a + b + c + d + e)/5
(b + c + d + e + f)/5
(c + d + e + f + g)/5
(d + e + f + g + h)/5
(e + f + g + h + i)/5
(f + g + h + i + j)/5
(g + h + i + j + k)/5
(3 + i + j)/3
1



  • Prev by Date: Re: Alaska
  • Next by Date: Wrapping NDSolve within a function
  • Previous by thread: Re: moving average function
  • Next by thread: Re: moving average function