[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: "Looking back" within a singe vector

From: Francesco Potorti`
Subject: Re: "Looking back" within a singe vector
Date: Sun, 15 Mar 2009 01:34:11 +0100

>I have a single vertical vector containing several thousand entries and what
>I would like to do is loop through the vector and calculate various "moving"
>calculations, e.g. at v(100,1) I would like to calculate a value for v(80,1)
>to v(100,1) inclusive, the calculations typically being averages

average is a linear operation, the simplest way is using the 'filter'
function, or else you can use 'cumsum'.

>deviations, maximums and minimums and Fisher transformations. 

These are not linear, so I do not see how you can use 'filter' here.

For standard deviation you can first square the vector, then do the same
as for average.

I do not have any ideas for the others.  In general, however, provided
you have enough memory, you can build a matrix like this (untested):
 M = repmat(V,21,1);
 for ii=2:21 M(ii,:) = shift(M(ii,:),1-ii); endfor
 M = M(:,1:end-20);
and then apply your transformations to M's columns.

>A further complication arises in
>that the length of the look back period varies according to data contained
>in a second vector.

All the above suggestions assume the the lookback period is constant.
However, if 20 is the maximum lookback period, you can build a matrix M
as in the last example, then invalidate an initial number of samples in
each column depending on the lookback period before applying your
transformations.  One way of doing this is to set them the invalid
values to NA and then apply your transformations to the columns using
the 'nanmean' and similar functions form the statistics package or the
'mean' and similar functions from the nan package.

All the above is very terse and untested, so ask for more info if this
isnot clear.

Francesco Potortì (ricercatore)        Voice: +39 050 315 3058 (op.2111)
ISTI - Area della ricerca CNR          Fax:   +39 050 315 2040
via G. Moruzzi 1, I-56124 Pisa         Email: address@hidden
(entrance 20, 1st floor, room C71)     Web:

reply via email to

[Prev in Thread] Current Thread [Next in Thread]