MATLAB Answers

0

Distance formula using while loop

PetronasAMG 님이 질문을 제출함. 2 Feb 2018
최근 활동 Jan 님이 편집함. 4 Feb 2018
so i given a data file, I was able to call in these values into Matlab input
these cells are empty because user chooses what values to input I have to call out 5 values for each column fo i (Xa,Ya,Xb,Yb, and t) so will using
while i <= 8
Di = sqrt((Xb-Xa)^2+(Yb-Ya)^2))
end
will this code give me D1 to D8? also how would I be able to multiple each t(i) into to corresponding Dis???

  댓글 수: 0

로그인 to comment.

태그

답변 수: 2

Jan 님의 답변 4 Feb 2018
Jan 님이 편집함. 4 Feb 2018
 채택된 답변

Do not create a set of variables called "D1, D2, ...". See
and with much more details:
This is more efficient (posted by Youssef already):
D = sqrt((Xb - Xa) .^ 2 + (Yb - Ya) .^ 2))
Then use D(2), D(2), ... instead of D1, D2, ...

  댓글 수: 0

로그인 to comment.


Youssef Khmou 님의 답변 2 Feb 2018

In the given formulae, are the variables vectors? if it is the case, then you need to use index in the loop for each variable to select the ith value at ith iteration:
while ii<=8
D(ii)=sqrt((Xb(ii)-Xa(ii))^2+(Yb(ii)-Ya(ii))^2);
ii=ii+1;
end
in compact form you can directly compute the distance using element wise operation:
D=sqrt(Xa-Xb).^2+(Yb-Ya).^2);
to multiply each value of D with its corresonding value of t use the compact form:
R=D.*t;

  댓글 수: 0

로그인 to comment.



Translated by