Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
1  
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

3 Answers 3

up vote 9 down vote accepted

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.

share|improve this answer
    
ListCorrelate looks to be exactly what I'm looking for. –  Mr Alpha Feb 17 '12 at 8:45

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"
 ]
share|improve this answer

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}
*)
share|improve this answer
    
And I assumed it is a vector-variable which depends on the time.. –  halirutan Feb 16 '12 at 22:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.