How do I make this for loop
1 次查看(过去 30 天)
显示 更早的评论
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
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.
回答(1 个)
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Standard File Formats 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!