필터 지우기
필터 지우기

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
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 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