How do I make this for loop

1 view (last 30 days)
Nanny Aberd
Nanny Aberd on 25 May 2019
Edited: Nanny Aberd on 26 May 2019
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 Comments
Geoff Hayes
Geoff Hayes on 25 May 2019
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 on 26 May 2019
oh sorry..
I mean
x10 = point1(1)-point2(1);
y10 = point1(2)-point2(2);
x20 = point3(1)-point2(1);
y20 = point3(2)-point2(2);

Sign in to comment.

Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 26 May 2019
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 Comment
Nanny Aberd
Nanny Aberd on 26 May 2019
Edited: Nanny Aberd on 26 May 2019
Thank you Sir.
In your code ang = [14 162 126 0]
but result that I want ang = [126 14 162 104 107]

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!