How to write a code for an equation in such a way that it add previous results repetitively?
2 次查看(过去 30 天)
显示 更早的评论
Hi
I want calculate distance between to points as:
X1 = X(1:end-1); X2 = X(2:end);
Y1 = Y(1:end-1); Y2 = Y(2:end);
xi = 5.55e5;
x = xi + sqrt((X2-X1).^2 + (Y2-Y1).^2);
But I want like say first value of x is = xi (5.55e5) and in the second value of x, xi will be added and in the next value the previoous one will be add and so on... I mean after first value equation of x will move in a way that the previous one will be added into the results. How can I write x equation in matlab? For example
if X = [2 3 4 5 6 7 8 9];
X1 = X(1:end-1); X2 = X(2:end);
xi = 5.55e5;
Now if I compute x as (assuming Y2 = Y1, and Y2-Y1 is 0.
x1 = 5.55e5 + sqrt(3 - 2).^2
x1 = = 555001;
x2 = x1 + sqrt(4 - 3).^2
and so on....
How to write in MATLAB equation/code?
4 个评论
采纳的回答
Torsten
2022-11-15
xi = 5.55e5;
X = ...;
Y = ...;
x = [xi,sqrt((X(2:end)-X(1:end-1)).^2 + (Y(2:end)-Y(1:end-1)).^2)]; % assumes X and Y are row vectors
x = cumsum(x)
0 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!