I am mistaken for angle
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Jan
le 2 Mar 2017
Modifié(e) : Jan
le 2 Mar 2017
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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur GPU Computing in MATLAB dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!