How do I make this for loop

1 Ansicht (letzte 30 Tage)
Nanny Aberd
Nanny Aberd am 25 Mai 2019
Bearbeitet: Nanny Aberd am 26 Mai 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 Kommentare
Geoff Hayes
Geoff Hayes am 25 Mai 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 am 26 Mai 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);

Melden Sie sich an, um zu kommentieren.

Antworten (1)

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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Standard File Formats finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by