Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Vectorize a 'rolling' calculation (like a moving avg)

450 views
Skip to first unread message

happydude

unread,
Nov 9, 2009, 11:17:03 AM11/9/09
to
I have a column vector (Vector "A") filled with 2000 random numbers between -100 and 100.

1. I want to create a row in "Vector B" for each row in Vector A. The new row will hold the average of the positve values in the previous 50 rows of Vector A.

2. I also want to create Vector C to hold the standard deviation of the same previous 50 rows.

How can I do this without using a for loop? and will it be more efficient/faster?

someone

unread,
Nov 9, 2009, 12:05:20 PM11/9/09
to
"happydude " <hud...@hotmail.com> wrote in message <hd9f9v$k9m$1...@fred.mathworks.com>...

% Does the first example in:

doc filter

% help you?

happydude

unread,
Nov 9, 2009, 12:17:19 PM11/9/09
to
thanks, yes, i'm looking at this now.. it has me scratching my head a bit! :)

happydude

unread,
Nov 9, 2009, 12:32:03 PM11/9/09
to
thanks for your help...

I'm not sure if/how i could use the filter function to help calc rolling standard deviation?

also, how would
1. i extract a rolling sum?
2. make a new column with each row based on the immediately previous row (e.g. equal to the row before it plus a random number).

someone

unread,
Nov 9, 2009, 1:39:04 PM11/9/09
to
"happydude " <hud...@hotmail.com> wrote in message <hd9jmj$pht$1...@fred.mathworks.com>...

> thanks for your help...
>
> I'm not sure if/how i could use the filter function to help calc rolling standard deviation?

% Download and try "moving standard deviation" form the MATLAB FEX at:

http://www.mathworks.com/matlabcentral/fileexchange/9428-moving-window-standard-deviation

>
> also, how would
> 1. i extract a rolling sum?

% Look at the source code in the above.
% It should give you a clue.

> 2. make a new column with each row based on the immediately previous row (e.g. equal to the row before it plus a random number).

% Is the desired first column just random numbers?
% If so, then my idea would be to create a matrix of random numbers
% (call it x) whose size has one more column than the original matrix.
% Then create another matrix such that:
b = [ 0 a]
% where a is the original matrix and 0 is a column of all zeros.
% then y = x + b should be your desired result.

ImageAnalyst

unread,
Nov 9, 2009, 4:39:53 PM11/9/09
to

-----------------------------------------------------------------------------
It prettymuch gives it to you right in the documentation, where it
says:

"Example

You can use filter to find a running average without using a for loop.
This example finds the running average of a 16-element vector, using a
window size of 5.

data = [1:0.2:4]';
windowSize = 5;
filter(ones(1,windowSize)/windowSize,1,data)

ans =
0.2000
0.4400
0.7200
1.0400
1.4000
1.6000
1.8000
2.0000
2.2000
2.4000
2.6000
2.8000
3.0000
3.2000
3.4000
3.6000"

Lars

unread,
Nov 10, 2009, 11:20:18 AM11/10/09
to
"someone" <som...@somewhere.net> wrote in message <hd9nk8$50h$1...@fred.mathworks.com>...

> "happydude " <hud...@hotmail.com> wrote in message <hd9jmj$pht$1...@fred.mathworks.com>...
> > thanks for your help...
> >
> > I'm not sure if/how i could use the filter function to help calc rolling standard deviation?
>
> % Download and try "moving standard deviation" form the MATLAB FEX at:
>
> http://www.mathworks.com/matlabcentral/fileexchange/9428-moving-window-standard-deviation


Or have a look at this message:

<http://groups.google.se/group/comp.soft-sys.matlab/msg/e0d4e06cf8a703f0?hl=en>

that provides general advice on how to use filter to calculate various stats.

hth
Lars

happydude

unread,
Nov 10, 2009, 12:53:03 PM11/10/09
to
thanks all .. helped a lot !

anyone know how i might calculate rolling downside deviation?

0 new messages