Problems with Conditionally execute statements

2 次查看(过去 30 天)
Hi, I'm having problems using if, elseif, and else. I'm trying to execute the statements for a matrix of satelite wind data... I pass from cartesian to polar coordinates and then from radians (on angle theta, the direction of the wind vector) to degrees, and I realized that matlab points the zero towards east in the rose (compass function). I want to make a conversion so that a theta angle of zero (or 360) points north, and 90 ° points east. I wrote this lines of code to do that... the weird thing is that the conditionall statements work when I use single values of theta, but when I run it for the whole matrix, it doesn't work, and it uses THE LAST (ELSE) statement, instead of my ELSEIF.. Any help with that? or any other way to make the conversion that I'm trying to do? Any matlab function to do it? Thanks a lot!
if tm < 0
tm = abs(tm)+90;
elseif tm > 90
tm=360+90-(tm);
else
tm=abs(tm-90);
end
My data matrix (tm, already in degrees):
tm = [-159.3884 -173.0609 -174.9012 -178.0744 164.3687 -179.7163 NaN NaN NaN
-161.7762 -179.5870 173.2305 176.2090 158.6573 177.0568 NaN NaN NaN
-163.5107 -174.3112 165.9708 169.3241 152.4023 169.0777 NaN NaN NaN
-170.2709 174.9133 178.0332 169.5016 150.9017 169.8905 NaN NaN NaN
179.7075 174.8766 168.3010 150.7951 143.1325 NaN NaN NaN NaN
165.8388 165.0565 162.8236 140.7707 NaN NaN NaN NaN NaN
152.2543 147.3183 -169.1261 NaN NaN NaN NaN NaN NaN
133.4060 131.5305 NaN NaN NaN NaN NaN NaN NaN]
  2 个评论
Azzi Abdelmalek
Azzi Abdelmalek 2012-9-8
your vector tm contains many values, what do you mean by
if tm<0
do you mean all values<0?
Millo Marin
Millo Marin 2012-9-11
Yes, because for some reason (which I don't know) matlab gives negative degrees in angles that are > 180°, and positive in angles < 180°. Thank you for your reply!

请先登录,再进行评论。

采纳的回答

Matt Fig
Matt Fig 2012-9-8
编辑:Matt Fig 2012-9-8
for ii=1:numel(tm)
if tm(ii) < 0 % Need to look at each element one-at-a-time!
tm(ii) = 90-tm(ii);
elseif tm(ii) > 90
tm(ii) = 450-(tm(ii));
else
tm(ii) = abs(tm(ii)-90);
end
end
I encourage you to read that article by Loren, and click on the comments she refers to in the first sentence.

更多回答(1 个)

Azzi Abdelmalek
Azzi Abdelmalek 2012-9-8
编辑:Azzi Abdelmalek 2012-9-8
% maybe is that what you need
for k=1:length(tm)
if tm(k)< 0
tm(k) = abs(tm(k))+90;
elseif tm(k) > 90
tm(k)=360+90-tm(k);
else
tm(k)=abs(tm(k)-90);
end
end
  1 个评论
Millo Marin
Millo Marin 2012-9-11
That was the idea, except that length function gives you the number or columns in a matrix, and I needed to loop for each of the values in the matrix. Thank you for your reply!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Reference Applications 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by