help-octave
[Top][All Lists]

## Re: "Looking back" within a singe vector

 From: Jaroslav Hajek Subject: Re: "Looking back" within a singe vector Date: Sun, 15 Mar 2009 09:19:17 +0100

```On Sat, Mar 14, 2009 at 6:19 PM, babelproofreader
>
> 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, standard
> deviations, maximums and minimums and Fisher transformations. Once done,
> move forward and repeat for v(101,1) etc. A further complication arises in
> that the length of the look back period varies according to data contained
> in a second vector. Can anyone suggest what functions I should be looking at
> to complete such a task?

Unless you can and want to reuse parts of the computations as the
slice progresses, you can do:
ilb = ... look back indices ....

slice = cell (1, length (v));
for i = 1:n
slice{i} = v(ilb(i):i);
endfor

and now use cellfun to apply whatever reductions you want to the
slices, e.g. sum, mean, max...
It would be nice if the loop could be vectorized, but I don't see a
way unless we write a new compiled function. It can be done with
cellfun and an anonymous function, but I don't think it will be
faster.
Anyway, with just several thousand data it runs quite fast on my machine.
The nice advantage of this approach is that with Octave 3.2, you'll
find out that the slices are not actually copied unless you attempt to
modify them; the "slice" cell array will just hold references to the
vector. This will make it memory efficient, especially if the slices
can grow large.

hth

--
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz

```