# Transform vector with lag

Say I have a vector $Z_n$ with and I want to transform it into another vector using this formula:

$X_t=Z_t+0.7Z_{t-2}$

The problem is that I am not quite sure how to get that $t-2$ lag in there. I see how I could do it with a For loop but that doesn't seem like a very Mathematica way of doing it.

-
The question is not very clear. What do mean by this lag in a vector addiction? If you are talking about some recurrence relation you need to give initial conditions for that. –  PlatoManiac Feb 16 '12 at 21:39

Assuming that the vector is called z and $n$ in your formula is the integer index of vector elements, use

x = Drop[z, 2] + 0.7 Drop[z, -2]


This adds the vector (sans the last two elements) to the vector times 0.7 with the first two elements removed (i.e. shifted by two elements to the left). Of course the result will be 2 elements shorter than the original. If this is not desired, you need to decide what $Z_0$ and $Z_{-1}$ should be and pad (PadLeft) z with these values.

This is likely the fastest possible solution in Mathematica.

EDIT

Another, also very fast possibility is

x = ListCorrelate[{0.7, 0, 1}, z]


ListCorrelate has settings for padding the arrays as well, if needed.

-
ListCorrelate looks to be exactly what I'm looking for. –  Mr Alpha Feb 17 '12 at 8:45

Your formula is not really correct. It lags of initial conditions but maybe this example helps you:

z[0] = {1, 2, 3};
z[1] = {4, 4, 4};
z[2] = {3, 2, 1};
z[n_ /; n > 2] := z[n - 1] + 0.7 z[n - 2]

z[5]

(*
Out[8]= {11.96, 9.56, 7.16}
*)

-
And I assumed it is a vector-variable which depends on the time.. –  halirutan Feb 16 '12 at 22:07

If by "delay" you mean you're trying to implement some equation of motion, maybe this will help:

Manipulate[
v = v + .1 (p - q) - .5 v;
q = q + .1 v;
Graphics[{Red, Point[q]}, PlotRange -> {{-1, 1}, {-1, 1}}],
{{p, {0, 0}}, {-1, -1}, {1, 1}, Locator},
ControlPlacement -> Automatic,
SynchronousUpdating -> True, SaveDefinitions -> True,
Initialization :> (q = {0, 0}; v = {0, 0}),
FrameLabel -> "The red dot will follow you with some delay"
]

-