Writing a basic formula - need assisstance

Hi all,
I am really struggling to right a basic formula. I am hoping someone can aid me with this.
I have:
Var1 = 40000 x 1 matrix
and i would like to create a matrix, Var2 with a formula, which i am struggling with. The formula i need to transpose onto matlab is as follows:
Var2_n = (Var1_n - Var1_n+1) + Var2_n-1`
I hope that is clear.
I do know the first part of the equation can be quantified utilising the diff() function, however I am unsure how to incorporate the second part of the equation.
Any help would be much appreciated.

3 Comments

Var2_n = (Var1_n - Var1_n+1) + Var2_n-1` ?? this means Var2(n) = Var1(n)-Var1(n+1)+Var2(n-1)???If so you should be having intital condition for Var2, do you have it?
There you go, i made a mistake in the explanation already.
No! I have not computed Var2 yet. Should i split this up into two formulae?
You need a starting condition, because the calculation of the next value depends on the previous. In my answer I assumed the starting condition to be 0.
Therefor, it will not be possible to split this up into two formulas. Recursive formulas are easiest to solve with a loop, but sometimes they can be linearized.

Sign in to comment.

Answers (1)

You may be able to play around with functions like cumsum, but I'm afraid you will have to use a loop:
Var1=rand(4000,1);%just so I have some data to work with
Var2=zeros(size(Var1));%pre-allocate
for n=2:(length(Var1)-1)
Var2(n)=(Var1(n)-Var1(n+1))+Var2(n-1);
end

1 Comment

This is of course a massive waste of time if you can reduce this problem mathematically, so do try that.

Sign in to comment.

Categories

Asked:

on 17 May 2017

Commented:

Rik
on 17 May 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!