Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #9930

Re: moving average function

From Ray Koopman <koopman@sfu.ca>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: moving average function
Date 2012-05-03 08:41 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jntgb3$2j$1@smc.vnet.net> (permalink)
References <jnlj94$n3k$1@smc.vnet.net> <jnoa47$547$1@smc.vnet.net> <jnqvr0$j73$1@smc.vnet.net>

Show all headers | View raw


On May 2, 2:47 am, rtmphone09@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

Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

moving average function Robert McHugh <rtmphone09@gmail.com> - 2012-04-30 08:42 +0000
  Re: moving average function Ray Koopman <koopman@sfu.ca> - 2012-05-01 09:24 +0000
    Re: moving average function rtmphone09@gmail.com - 2012-05-02 09:47 +0000
      Re: moving average function Ray Koopman <koopman@sfu.ca> - 2012-05-03 08:41 +0000
    Re: moving average function rtmphone09@gmail.com - 2012-05-03 08:34 +0000
  Re: moving average function "Oleksandr Rasputinov" <oleksandr_rasputinov@ymail.com> - 2012-05-01 09:25 +0000

csiph-web