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 CenterFile Exchange에서 Standard File Formats에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by