I am mistaken for angle
1 次查看(过去 30 天)
显示 更早的评论
Dx=[451.1489;71.922;22.064;-52.849;-434.247] Dy=[423.253;290.384;292.965; 296.519;377.443]
[u m]=size(Dx);
for i=1:u t(i)=atan(Dy(i)/Dx(i))*200/pi; end
if Dx(i)>0 & Dy(i)>0 t(i)=t(i) elseif Dx(i)<0 & Dy(i)>0 t(i)=t(i)+200 elseif Dx(i)<0 & Dy(i)<0 t(i)=t(i)+200; elseif Dx(i)>0 & Dy(i)<0 t(i)=t(i)+400; end
The result is wrong; t= [47.970; 84.543; 95.214; -88.771; 154.448]
The correct result is; t= [47.970; 84.543; 95.214; 111.229; 154.448]
Is line 4 causing the error?
0 个评论
采纳的回答
Jan
2017-3-2
编辑:Jan
2017-3-2
Note that you change the result outside the loop:
for i=1:u
t(i)=atan(Dy(i)/Dx(i))*200/pi;
end % <-- remove this
if Dx(i)>0 & Dy(i)>0
t(i)=t(i) % Omit this one :-)
elseif Dx(i)<0 & Dy(i)>0
t(i)=t(i)+200
elseif Dx(i)<0 & Dy(i)<0
t(i)=t(i)+200;
elseif Dx(i)>0 & Dy(i)<0
t(i)=t(i)+400;
end
% end % <-- insert an "end" here
When processed after the loop, only the last value of "i" is accessed.
Logical indexing will be more efficient:
t = atan(Dy ./ Dx) * (200/pi); % elementwise: ./
index = (Dx<0 & Dy>0);
t(index) = t(index) + 200;
and so on.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 GPU Computing in MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!