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?

采纳的回答

Jan
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 CenterFile Exchange 中查找有关 GPU Computing in MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by