Writing a basic formula - need assisstance
Mostrar comentarios más antiguos
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 comentarios
KSSV
el 17 de Mayo de 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
el 17 de Mayo de 2017
Rik
el 17 de Mayo de 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.
Respuestas (1)
Rik
el 17 de Mayo de 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 comentario
Rik
el 17 de Mayo de 2017
This is of course a massive waste of time if you can reduce this problem mathematically, so do try that.
Categorías
Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!