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!