Writing a basic formula - need assisstance
Show older comments
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
KSSV
on 17 May 2017
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?
Theodore Bowen
on 17 May 2017
Rik
on 17 May 2017
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.
Answers (1)
Rik
on 17 May 2017
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
Rik
on 17 May 2017
This is of course a massive waste of time if you can reduce this problem mathematically, so do try that.
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!