How to caculate a value for a step - the previous step

조회 수: 5 (최근 30일)
Nikolaos Zafirakis
Nikolaos Zafirakis 2019년 5월 3일
댓글: M 2019년 5월 6일
So, I have a value x and i want a piece of code which would allow me to take x_current - x_one_step_previously for example at iteration 50 i would want x_50-x_49 is there a way to do this in Matlab. Thanks in advance to anyone who helps!!

답변 (2개)

M
M 2019년 5월 3일
It depends on what you mean by "I have a value x".
It is actuallyquite easy to do for a straightforward example, but it depends on your applications.
Could you provide more details about what you are trying to do ?
Otherwise here is an example:
x = zeros(1,50);
for i = 1 :50;
x = i;
end
a = x (50) - x(49)
  댓글 수: 2
Nikolaos Zafirakis
Nikolaos Zafirakis 2019년 5월 3일
So my x value is a vector 1x3 and i need to do (x_current-x_one_step_previous)/time
say the time=100 seconds.
M
M 2019년 5월 6일
x is a vector 1x3, you can still save its value in another vector, let's call is x_saved, a vector of dimension 3 x 100.
Would something like the following example work ?
x_saved = zeros(3,100);
for i = 1 : 100
x = ... ; vector 1 x 3
x_saved(:,i) = x;
if i ~= 1
delta = x(:,i)-x(:,i-1);
end
end

댓글을 달려면 로그인하십시오.


Steven Lord
Steven Lord 2019년 5월 3일
If you have numbered variables (which you shouldn't) then it will be painful.
If you have elements in an array, it's easy.
x = (1:20).'.^2;
dx = diff(x);
Note that dx has one fewer element than x, so if you want to display them together you'll need to augment it with an extra element like so:
dx2 = [0; dx];
t = table(x, dx2, 'VariableNames', {'x', 'delta_x'})
  댓글 수: 1
Nikolaos Zafirakis
Nikolaos Zafirakis 2019년 5월 3일
Hello Steven,
I think you have misunderstood my question, hope this example clears it up. I look forward to your response.
kind regards,
Nikolaos
for i = 0:t
x(i) = (x1(i) - x2 /t1(i) -t2); % x2 and t2 are the state at the previous interval
end % so if at t = 20 i will have x(i) = (x1(20) - x2(19) /t1(20) -t2(19))

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by