How to write a code for an equation in such a way that it add previous results repetitively?

1 回表示 (過去 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
Torsten 2022 年 11 月 15 日
編集済み: Torsten 2022 年 11 月 15 日
And Y has disappeared in the example ?

サインインしてコメントする。

採用された回答

Torsten
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 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by