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 个评论
KSSV
2017-5-17
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
2017-5-17
Rik
2017-5-17
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.
回答(1 个)
Rik
2017-5-17
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 个评论
Rik
2017-5-17
This is of course a massive waste of time if you can reduce this problem mathematically, so do try that.
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!