How do I make this for loop

1 回表示 (過去 30 日間)
Nanny Aberd
Nanny Aberd 2019 年 5 月 25 日
編集済み: Nanny Aberd 2019 年 5 月 26 日
I have matrix 5x2
point = [105,308
162,470
200,150
470,135
570,390]
x10 = point1(1)-point2(1);
y10 = point3(2)-point2(2);
x20 = point1(1)-point2(1);
y20 = point3(2)-point2(2);
ang = atan2(abs(x10*y20-x20*y10),x10*y10+x20*y20)*180/pi;
by. point1 = previous point
point2 = center point
point3 = next point
I want to loop for finish 5 center point
  2 件のコメント
Geoff Hayes
Geoff Hayes 2019 年 5 月 25 日
Nanny - why is x20 and x10 identical? y10 and y20 are the same too.
Consider replacing your arrays for each point with one array where each row represents a point rather than having individual variables for each point.
Nanny Aberd
Nanny Aberd 2019 年 5 月 26 日
oh sorry..
I mean
x10 = point1(1)-point2(1);
y10 = point1(2)-point2(2);
x20 = point3(1)-point2(1);
y20 = point3(2)-point2(2);

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

回答 (1 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2019 年 5 月 26 日
Hi Nanny,
Here is the loop based code of your problem:
point = [105,308
162,470
200,150
470,135
570,390];
x=zeros(1, numel(point(:,1))-1);
y=x;
ang=x;
for ii = 1:numel(point(:,1))-1
x(ii) = point(ii,1)-point(ii+1, 1);
y(ii) = point(ii,2)-point(ii+1, 2);
end
for jj=1:numel(x)-1
ang(jj) = atan2(abs(x(jj)*y(jj+1)-x(jj+1)*y(jj)),x(jj)*y(jj)+x(jj+1)*y(jj+1))*180/pi;
end
Note that in your formualtion there are some points (flaws w.r.t x10, x20, y10, y20...) as Geoff has pinpointed that was corrected.
Good luck.
  1 件のコメント
Nanny Aberd
Nanny Aberd 2019 年 5 月 26 日
編集済み: Nanny Aberd 2019 年 5 月 26 日
Thank you Sir.
In your code ang = [14 162 126 0]
but result that I want ang = [126 14 162 104 107]

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

カテゴリ

Help Center および File ExchangeStandard File Formats についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by